SimpleForm maxlength 扩展的工作原理 [英] How SimpleForm maxlength extension works

查看:33
本文介绍了SimpleForm maxlength 扩展的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 SimpleForm gem 的帮助下创建的表单上设置输入的 maxlength html 属性.我知道我可以通过在创建表单时手动传入 maxlength 属性来做到这一点,例如:

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 } %>

但这不是我想要的,因为根据 SimpleForm 配置文件中的注释,您应该启用 maxlength 扩展,它会在给出最大长度验证时自动将此 html 属性添加到字符串属性的输入标记中.

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

不幸的是,上述两种可能性均无效.我是否完全误解了 maxlength 扩展的使用?

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

推荐答案

是正确的,当编辑simple_form.rb配置文件为

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.

我将 simple_form 与 Twitter Bootstrap 结合使用.为了对表单产生这种效果,我还必须在配置文件 simple_form_bootstrap.rb 中插入这一行

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

这篇关于SimpleForm maxlength 扩展的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆