让自定义验证器与 client_side_validations gem 一起使用
我已经编写了一个自定义验证器,并正在寻找让它与 client_side_validations gem 一起工作的方法。现在它仍然允许我提交表单,然后我收到错误框和错误消息。我想知道是否有办法修改 gem 或者我需要如何格式化验证。
这是我问的关于如何设置验证的最后一个问题:在 Rails 3 中编写自定义验证器< /a>
是用正则表达式表达最大字数的最佳方法吗?
I've written a custom validator and am looking for ways to get it work with the client_side_validations gem. Right now it still allows me to submit my form and then I get a the error box and error messages. I was wondering if there is a way to modify the gem or how I need to format validation.
Here's the last question I asked about how to setup my validation: Writing custom validator in Rails 3
Is the best way to do this to express the maximum word count in terms of a regular expression?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
wiki 有关于如何编写自定义验证器的非常详细的信息: https://github.com/ bcardarella/client_side_validations/wiki/Custom-Validators
您需要遵循 wiki 上的指南,了解如何以与 ClientSideValidations gem 配合使用的方式编写 Rails 验证器。例如,如果我在您的其他帖子上使用了验证器,我会这样写:
然后您需要将 :word_limit 的错误消息添加到您的 en.yml 文件中:
最后您需要为JavaScript。我通常创建一个名为rails.validations.custom.js 的新文件,这是我一直在使用的约定,因为我不想弄乱核心文件。
那应该可以了。如果您复制/粘贴,我可能会犯一些语法错误,因为我是凭空做的。
说实话,为什么不直接使用 Rails 格式验证器呢?您可以在您的模型中执行此操作:
这实际上会得到同样的结果。
The wiki has pretty detailed information on how to write custom validators: https://github.com/bcardarella/client_side_validations/wiki/Custom-Validators
You need to follow the guide on the wiki for how to write the Rails validator in such a way that it works with the ClientSideValidations gem. For example, if I took the validator on your other post I would write it like this:
You'll then need to add the error message for :word_limit to your en.yml file:
Finally you'll need to add the validator for the Javascript. I usually create a new file called rails.validations.custom.js It's the convention I've been using as I don't want to mess around with the core file.
And that should do it. If you're copy/pasting I may have made some syntax errors as I'm doing this off the top of my head.
To be honest though, why not just use the Rails format validator? You could do this in your model:
And that effectively gets you the same thing.
另一件事是,你的正则表达式可能在 Javascript 中工作,也可能不工作。 Javascript 和 Ruby 有不同的正则表达式引擎,虽然大多数东西都可以在两者中工作,但并非所有东西都可以。因此,您可能需要测试 javascript 中的表达式,看看是否得到了您想要的结果。如果没有,那么您需要在 javascript 验证器中使用不同的正则表达式。
One other thing, the Regular Expression you have may or may not work in Javascript. Javascript and Ruby have different Regular Expression engines and while most things work in both not all things do. So you may have to test the expression in javascript to see if you're getting the results you want. If not then you'll need to the have a different Regex in the javascript validator.
我刚刚添加了上述功能。如果您将 Gemfile 指向 Github 上的主分支,您应该能够按照我描述的方式有条件地强制验证器。
I've just added the said functionality. If you point your Gemfile to the master branch on Github you should be able to conditionally force validators in the manner I described.