使用 jQuery 验证插件时,如何在 Ruby on Rails 生成的表单字段上使用客户错误消息?
我正在尝试使用 jQuery Validation 插件,并以表单形式显示客户错误消息在我的网站上。指定客户消息的方法如下:
$('#user_new').validate({
messages: {
user_email: {
required: "dd ",
email: "Please enter a valid email address, example: [email protected]"
}
}
});
这会将自定义错误消息应用于名称为 user_email
的输入。这段代码工作正常。问题在于这些字段的名称如 user[email]
,因为它们是由 Rails 使用 form_for
生成的。但这是行不通的:
$('#user_new').validate({
messages: {
user[email]: {
required: "dd ",
email: "Please enter a valid email address, example: [email protected]"
}
}
});
user[email]
部分会导致 JavaScript 错误。有没有人使用这个插件在 Rails 生成的输入字段上使用自定义消息?感谢您的阅读。
I'm trying to use jQuery Validation plugin with customer error messages in a form on my site. The way to specify customer messages is as follows:
$('#user_new').validate({
messages: {
user_email: {
required: "dd ",
email: "Please enter a valid email address, example: [email protected]"
}
}
});
This will apply the custom error messages to the input with the name of user_email
. This code works okay. The problem is that the fields have names such as user[email]
because they are generated by Rails using form_for
. But this won't work:
$('#user_new').validate({
messages: {
user[email]: {
required: "dd ",
email: "Please enter a valid email address, example: [email protected]"
}
}
});
The user[email]
part causes a javascript error. Has anyone got custom messages working on Rails generated input fields with this plugin? Thanks for reading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过仅引用 javascript 数组键?
Have you tried just quoting the javascript array keys?