HHC
HHC

Reputation: 2563

Is there a way to set required attribute on text_field_tag?

In text_field one can do the following to set the required attribute.

< %= f.text_field :street, :required => true % >

< input id="recipe_name" name="recipe_name" type="text" required >

However, with text_field_tag, if I do the same the output html sets the value attribute instead, which is not correct.

< %= text_field_tag :street, :required => true % >

output:

< input id="recipe_name" name="recipe_name" type="text" value="{:required=>true}" >

Is required not supported in text_field_tag? What is a good way to work around it?

Upvotes: 25

Views: 21910

Answers (2)

shweta
shweta

Reputation: 8169

Try: text_field_tag(name, value = nil, options = {})

<%= text_field_tag :street, nil, :required => true %>

When you provide options to the helper, you have to pass the value for value parameter.

Upvotes: 52

Oleg Haidul
Oleg Haidul

Reputation: 3732

Text field tag

Try this:

<%= text_field_tag :street, '', :required => true %>

Upvotes: 2

Related Questions