javascript Rails 4 客户端验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21050669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:55:36  来源:igfitidea点击:

Rails 4 client side validation

javascriptruby-on-railsrubyruby-on-rails-4client-side-validation

提问by tblakey89

Since upgrading my site to Rails 4, the client-side-validations gem has stopped working for me. I have had a look at the various alternatives such as jquery-validation-rails and judge, which involve writing quite a bit of javascript, especially for a site so large. And in the case of jquery-validation-rails, re-doing all the validation rules client side. Considering Rails is such a popular framework, it surprises me that there is no 'easy' option. Is there any gem which anyone else is aware of, which does validation as well as the client-side-validation gem?

自从将我的网站升级到 Rails 4 后,客户端验证 gem 已停止对我来说有效。我查看了各种替代方案,例如 jquery-validation-rails 和 Judge,它们涉及编写相当多的 javascript,尤其是对于如此大的站点。在 jquery-validation-rails 的情况下,重新执行客户端的所有验证规则。考虑到 Rails 是一个如此流行的框架,让我感到惊讶的是没有“简单”的选择。是否有其他人知道的任何 gem,它进行验证以及客户端验证 gem?

Or is the best alternative to use the ModelName.validators method mixed with some ajax, and do the validation through the jquery-validation-rails gem?

或者是将 ModelName.validators 方法与一些 ajax 混合使用并通过 jquery-validation-rails gem 进行验证的最佳替代方法?

采纳答案by Beartech

From the Google.

来自谷歌。

http://rubygems.org/gems/rails4_client_side_validations

http://rubygems.org/gems/rails4_client_side_validations

https://github.com/amatsuda/html5_validators

https://github.com/amatsuda/html5_validators

Also this guy seemed to have gotten the same gem you are using to work by using the 4-0-beta branch.

此外,这个家伙似乎已经通过使用 4-0-beta 分支获得了您用来工作的相同 gem。

Client Side Validations and Rails4

客户端验证和 Rails4

If the 4-0-beta branch works for you, consider helping to maintain the gem to keep it alive.

如果 4-0-beta 分支适合您,请考虑帮助维护 gem 以使其保持活力。

To use a certain git repo version in your Gemfile:

要在您的 Gemfile 中使用某个 git repo 版本:

gem 'client-side-validations', :git => 'https://github.com/bcardarella/client_side_validations/tree/4-0-beta'

回答by Roman

While the client side validations will work for the simple cases, they'll require an enormous amount of workarounds to work with more complicated scenarios (for instance - conditional validations). I would recommend using Judgeinstead, which give you more control.

虽然客户端验证适用于简单的情况,但它们需要大量的解决方法来处理更复杂的场景(例如 - 条件验证)。我建议改用Judge,这会给你更多的控制。

回答by prodigerati

I was able to use a fork of the original client_side_vlaidations that is very active at the moment. I am using the latest version of simple_form, rails 4.1.8, and this client_side_validations. Simple setup with basic config.

我能够使用目前非常活跃的原始 client_side_vlaidations 的一个分支。我正在使用最新版本的 simple_form、rails 4.1.8 和这个client_side_validations。使用基本配置进行简单设置。

回答by Boomerange

I know the question is old, but how about good old require: "require"?

我知道这个问题很老了,但是老式的 require: "require" 怎么样?

For example in view:

例如在视图中:

<%= f.text_area :body, placeholder: "Write your review here...", class: "form-control", rows: "5", required: "required" %>

回答by alentichev

If you are using Bootstrap Form for Rails, you can give bootstrap_validator_rails a try.

如果你正在使用 Bootstrap Form for Rails,你可以试试 bootstrap_validator_rails。

https://github.com/huynhquancam/bootstrap_validator_rails

https://github.com/huynhquancam/bootstrap_validator_rails