简体   繁体   English

使用activerecord模型验证器进行Javascript验证

[英]Javascript validation using activerecord models validators

I have an activerecord model and set of validators to it, But I also have to provide front end validation. 我有一个activerecord模型和一组验证器,但我还必须提供前端验证。

Is there a gem or a smart way to use activerecord validators, for instance in jquery validation plugin, instead of writing it by hand? 是否有宝石或智能方法来使用activerecord验证器,例如在jquery验证插件中,而不是手工编写?

Thanks 谢谢

You can check client_side_validations 您可以检查client_side_validations

Regards. 问候。

Jquery has a validation library. Jquery有一个验证库。 Plus there are many others too. 另外还有很多其他的。 Google for Javascript form validation. Google for Javascript表单验证。

Unfortunately, some validations require more access to the business rules of your app in your browser/JS code. 遗憾的是,某些验证需要在浏览器/ JS代码中更多地访问应用程序的业务规则。 So you'll then have to weigh the costs of duplicating business logic in your JS vs the benefit of the immediate feedback to the user. 因此,您必须权衡JS中复制业务逻辑的成本与对用户的即时反馈的好处。

Just be sure not to skimp on implementing your business rules (and testing them) in your server-side sw first. 确保不要先在服务器端sw中实施业务规则(并测试它们)。 Remember that clients can never be trusted--even the clients that you write yourself! 请记住,客户永远不会被信任 - 即使是您自己编写的客户!

No need to duplicate the costs. 无需复制成本。 There are some gems/plugins around that duplicate the Model's validations in the client side. 有一些宝石/插件在客户端复制模型的验证。 yes, you'll have to do some client-side customizations still, but it should be very easy. 是的,您还必须进行一些客户端自定义,但它应该非常简单。

here's an example of such plugin: http://github.com/grimen/validatious-on-rails 这是一个这样的插件的例子: http//github.com/grimen/validatious-on-rails

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM