简体   繁体   中英

add maxlength to form_tag in rails

I can't seem to get this syntax working. I want to restrict this to a max length of 3 character.

<%= number_field_tag "foo[bar]" %>

My approach which isn't working is <%= number_field_tag "foo[bar]", :maxlength =>3 %>

thank you in advanced

You should be able to use it just fine as the number_field_tag accepts all the options of text_field_tag ( http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag ).

This typo should be the reason why it's not working.

Use :maxlength => 3 not :maxlenght => 3 .

Update:

The second parameter to the number_field_tag is value which you could either set to nil or empty as follows (As also pointed by MrYoshiji in his answer):

<%= number_field_tag "foo[bar]", '', :maxlength =>3 %>

According to the documentation:

You should use it this way:

number_field_tag "foo[bar]", nil, :max_length => 3

Or like this:

number_field_tag "foo[bar]", nil, :in => 0..999

<%= number_field_tag("foo[bar]", "some value", {maxlength: 3}) %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM