How SimpleForm maxlength extension works

≯℡__Kan透↙ 提交于 2020-01-03 18:11:12

问题


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?


回答1:


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
   ...


来源:https://stackoverflow.com/questions/15063500/how-simpleform-maxlength-extension-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!