目标 HTML 'name' 属性 rails 表单生成器 Jquery
target HTML 'name' attribute a rails form builder with Jquery
我根据教程 here 创建了一个多步骤表单。它在流程中集成了 Jquery 验证,以确保数据在到达服务器之前是干净的。验证和多步骤表单都按预期工作,但由于 Jquery 验证针对 HTML name
标签而不是 id
我必须手动更改它们才能让验证工作。
rules: {
registrant_first_name: {
required: true,
minlength: 2,
},
...
},
并在表单助手中:
<%= f.text_field :first_name, name: 'registrant_first_name', class: 'form-control' %>
然而,虽然这可行,但它会将我的参数发送出注册控制器,我需要重新配置模型以接受新参数。表单通过拒绝数据并表示字段为空白(技术上是这样)来响应
registrant_first_name: John
registrant_last_name: Doe
registrant_email: johndoe@msn.com
虽然我很确定我可以通过简单地重新配置注册人模型以接受参数来解决这个问题,但我想知道是否有办法配置 Jquery 验证规则以针对表单助手生成的名称属性? (目前它抛出一个错误)
rules: {
registrant[first_name]: {
required: true,
minlength: 2,
},
...
},
如果不是,配置模型以允许这些不再属于 registrants
哈希的参数是否有任何危害?
I'm wondering is there a way to configure the Jquery Validation rules to target the name attribute generated by the forms helper? (currently it throws an error)
是的。只需用引号将名称括起来。
rules: {
'registrant[first_name]': {
required: true,
minlength: 2,
....
Documentation: Fields with complex names (brackets, dots):
"If your form consists of fields using names that aren't legal JavaScript identifiers, you have to quote those names when using the rules
option"
我根据教程 here 创建了一个多步骤表单。它在流程中集成了 Jquery 验证,以确保数据在到达服务器之前是干净的。验证和多步骤表单都按预期工作,但由于 Jquery 验证针对 HTML name
标签而不是 id
我必须手动更改它们才能让验证工作。
rules: {
registrant_first_name: {
required: true,
minlength: 2,
},
...
},
并在表单助手中:
<%= f.text_field :first_name, name: 'registrant_first_name', class: 'form-control' %>
然而,虽然这可行,但它会将我的参数发送出注册控制器,我需要重新配置模型以接受新参数。表单通过拒绝数据并表示字段为空白(技术上是这样)来响应
registrant_first_name: John
registrant_last_name: Doe
registrant_email: johndoe@msn.com
虽然我很确定我可以通过简单地重新配置注册人模型以接受参数来解决这个问题,但我想知道是否有办法配置 Jquery 验证规则以针对表单助手生成的名称属性? (目前它抛出一个错误)
rules: {
registrant[first_name]: {
required: true,
minlength: 2,
},
...
},
如果不是,配置模型以允许这些不再属于 registrants
哈希的参数是否有任何危害?
I'm wondering is there a way to configure the Jquery Validation rules to target the name attribute generated by the forms helper? (currently it throws an error)
是的。只需用引号将名称括起来。
rules: {
'registrant[first_name]': {
required: true,
minlength: 2,
....
Documentation: Fields with complex names (brackets, dots):
"If your form consists of fields using names that aren't legal JavaScript identifiers, you have to quote those names when using the
rules
option"