使用 jQuery 验证插件时,如何在 Ruby on Rails 生成的表单字段上使用客户错误消息?

发布于 2024-09-30 10:55:06 字数 1075 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

知你几分 2024-10-07 10:55:07

您是否尝试过仅引用 javascript 数组键?

messages: {
    "user[email]": {
    required: "dd ",
    email: "Please enter a valid email address, example: [email protected]"
}

Have you tried just quoting the javascript array keys?

messages: {
    "user[email]": {
    required: "dd ",
    email: "Please enter a valid email address, example: [email protected]"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文