Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How SimpleForm maxlength extension works

I want to set the maxlength html attribute of the inputs on my forms created with the help of the SimpleForm gem. I know I can do this by passing in the maxlength attribute manually when creating the form, e.g.:

<%= f.input :username, input_html: { maxlength: 20 } %>

But that isn’t want I want because according the comments in the SimpleForm config file you should enable the maxlength extension which adds automatically this html attribute to the input tag for string attributes when a max-length validation is given.

## Optional extensions
# They are disabled unless you pass `f.input EXTENSION_NAME => :lookup`
# to the input. If so, they will retrieve the values from the model
# if any exists. If you want to enable the lookup for any of those
# extensions by default, you can change `b.optional` to `b.use`.

# Calculates maxlength from length validations for string inputs
b.use :maxlength

Unfortunately none of the 2 mentioned possibilities works. Did I misunderstand the use of maxlength extension completely?

like image 991
dan Avatar asked Dec 15 '22 13:12

dan


1 Answers

It is correct, when editing the simple_form.rb config file to

b.use :maxlength

the max-length in the form will use the value out of the model.

I use simple_form in conjunction with Twitter Bootstrap. To have this effect on the form, I also had to insert this line in the config file simple_form_bootstrap.rb

# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
   config.wrappers :bootstrap, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|

   # Calculates maxlength from length validations for string inputs
   b.use :maxlength
   ...
like image 184
crimi Avatar answered Dec 28 '22 03:12

crimi