formValidation jquery 插件不读取 rails 安全表单名称
formValidation jquery plugin does not read rails safe form names
我正在使用 formValidation plugin to validate my Rails 4 form. ,我必须将我的字段 "names" 从 'name="firstname"' 更改为 'name="user[firstname]"' 以便表单能够正确提交。但是,formValidation 插件依赖于使用字段的 "name" 而不是字段的 "id" 来 运行 验证代码。如果字段名称为 "firstname",我可以使 formValidation 工作,但如果名称为 "user[firstname]",则不能。有没有办法用括号在Javascript(这里是新手)中写一个"name"值?
这是我的表格。我试过了
<%= form_for(resource, :as => resource_name, id: 'new_user', :url => registration_path(resource_name)) do |f| %>
<% if resource.errors.any? %>
<%= devise_error_messages! %>
<% end %>
<%= f.text_field :firstname, :name=>'user[firstname]', :class => 'form-control', :autofocus => true, :required => true, :placeholder => 'FIRST NAME' %>
<%= f.text_field :lastname, :name =>'user[lastname]', :class => 'form-control', :autofocus => true, :required => true, :placeholder => 'LAST NAME ' %>
<%= f.email_field :email, :name=>'user[email]', :class => 'form-control ', :autofocus => true, :required => true, :placeholder => 'YOUR EMAIL', :style=>"width:100%" %>
<%= f.password_field :password, :class => 'form-control ', :name=>'user[password]', :autofocus => true, :required => true, :placeholder => 'YOUR PASSWORD' %>
<%= f.password_field :password_confirmation, :name=>'user[password_confirmation]', :class => 'form-control ', :autofocus => true, :required => true, :placeholder => 'CONFIRM YOUR PASSWORD' %>
<%= f.submit 'Create Account', :class => 'btn btn-aqua btn-lg btn-block',
:style => 'margin-bottom:5px' %>
<%end%>
<script>
$(document).ready(function() {
$('#new_user').formValidation({
framework: 'bootstrap',
icon: {
valid: 'fa fa-check',
invalid: 'fa fa-times',
validating: 'fa fa-refresh'
},
fields: {
user[firstname]: {
validators: {
notEmpty: {
message: 'Please enter your first name'
}
}
},
user[lastname]: {
validators: {
notEmpty: {
message: 'Please enter your last name'
}
}
},
user[email]: {
validators: {
notEmpty: {
message: 'The email address is required'
},
emailAddress: {
message: 'The input is not a valid email address'
}
}
},
user[password]: {
validators: {
notEmpty: {
message: 'The password is required'
},
}
},
user[password_confirmation]: {
validators: {
notEmpty: {
message: 'Please confirm your password'
},
identical: {
field: 'user[password]',
message: 'The passwords do not match'
}
}
}
button: {
// The submit buttons selector
selector: '[type="submit"]:not([formnovalidate])',
// The disabled class
disabled: 'disabled'
}
}
});
});
</script>
根据 docs for your validator plugin、
If the field name contains special characters such as ., [, ], you must wrap it between single or double quote. See the Validating field with special name example
所以只需将您的字段名称放在引号内,""
我的原始代码没有使用正确的命名格式 "names"...但我无法调试它,因为我在 "button" 正上方也遗漏了一个随机逗号。我正在发布工作代码:
<script>
$(document).ready(function() {
$('#new_user').formValidation({
framework: 'bootstrap',
icon: {
valid: 'fa fa-check',
invalid: 'fa fa-times',
validating: 'fa fa-refresh'
},
fields: {
'user[firstname]': {
validators: {
notEmpty: {
message: 'Please enter your first name'
}
}
},
'user[lastname]': {
validators: {
notEmpty: {
message: 'Please enter your last name'
}
}
},
'user[email]': {
validators: {
notEmpty: {
message: 'The email address is required'
},
emailAddress: {
message: 'The input is not a valid email address'
}
}
},
'user[password]': {
validators: {
notEmpty: {
message: 'The password is required'
},
}
},
'user[password_confirmation]': {
validators: {
notEmpty: {
message: 'Please confirm your password'
},
identical: {
field: 'user[password]',
message: 'The passwords do not match'
}
}
},
button: {
// The submit buttons selector
selector: '[type="submit"]:not([formnovalidate])',
// The disabled class
disabled: 'disabled'
}
}
});
});
</script>
我正在使用 formValidation plugin to validate my Rails 4 form.
这是我的表格。我试过了
<%= form_for(resource, :as => resource_name, id: 'new_user', :url => registration_path(resource_name)) do |f| %>
<% if resource.errors.any? %>
<%= devise_error_messages! %>
<% end %>
<%= f.text_field :firstname, :name=>'user[firstname]', :class => 'form-control', :autofocus => true, :required => true, :placeholder => 'FIRST NAME' %>
<%= f.text_field :lastname, :name =>'user[lastname]', :class => 'form-control', :autofocus => true, :required => true, :placeholder => 'LAST NAME ' %>
<%= f.email_field :email, :name=>'user[email]', :class => 'form-control ', :autofocus => true, :required => true, :placeholder => 'YOUR EMAIL', :style=>"width:100%" %>
<%= f.password_field :password, :class => 'form-control ', :name=>'user[password]', :autofocus => true, :required => true, :placeholder => 'YOUR PASSWORD' %>
<%= f.password_field :password_confirmation, :name=>'user[password_confirmation]', :class => 'form-control ', :autofocus => true, :required => true, :placeholder => 'CONFIRM YOUR PASSWORD' %>
<%= f.submit 'Create Account', :class => 'btn btn-aqua btn-lg btn-block',
:style => 'margin-bottom:5px' %>
<%end%>
<script>
$(document).ready(function() {
$('#new_user').formValidation({
framework: 'bootstrap',
icon: {
valid: 'fa fa-check',
invalid: 'fa fa-times',
validating: 'fa fa-refresh'
},
fields: {
user[firstname]: {
validators: {
notEmpty: {
message: 'Please enter your first name'
}
}
},
user[lastname]: {
validators: {
notEmpty: {
message: 'Please enter your last name'
}
}
},
user[email]: {
validators: {
notEmpty: {
message: 'The email address is required'
},
emailAddress: {
message: 'The input is not a valid email address'
}
}
},
user[password]: {
validators: {
notEmpty: {
message: 'The password is required'
},
}
},
user[password_confirmation]: {
validators: {
notEmpty: {
message: 'Please confirm your password'
},
identical: {
field: 'user[password]',
message: 'The passwords do not match'
}
}
}
button: {
// The submit buttons selector
selector: '[type="submit"]:not([formnovalidate])',
// The disabled class
disabled: 'disabled'
}
}
});
});
</script>
根据 docs for your validator plugin、
If the field name contains special characters such as ., [, ], you must wrap it between single or double quote. See the Validating field with special name example
所以只需将您的字段名称放在引号内,""
我的原始代码没有使用正确的命名格式 "names"...但我无法调试它,因为我在 "button" 正上方也遗漏了一个随机逗号。我正在发布工作代码:
<script>
$(document).ready(function() {
$('#new_user').formValidation({
framework: 'bootstrap',
icon: {
valid: 'fa fa-check',
invalid: 'fa fa-times',
validating: 'fa fa-refresh'
},
fields: {
'user[firstname]': {
validators: {
notEmpty: {
message: 'Please enter your first name'
}
}
},
'user[lastname]': {
validators: {
notEmpty: {
message: 'Please enter your last name'
}
}
},
'user[email]': {
validators: {
notEmpty: {
message: 'The email address is required'
},
emailAddress: {
message: 'The input is not a valid email address'
}
}
},
'user[password]': {
validators: {
notEmpty: {
message: 'The password is required'
},
}
},
'user[password_confirmation]': {
validators: {
notEmpty: {
message: 'Please confirm your password'
},
identical: {
field: 'user[password]',
message: 'The passwords do not match'
}
}
},
button: {
// The submit buttons selector
selector: '[type="submit"]:not([formnovalidate])',
// The disabled class
disabled: 'disabled'
}
}
});
});
</script>