#263 Client Side Validations
Showing validation errors inline as the user is filling out the form can lead to a better use experience. Learn how to do this using the Client Side Validations gem.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
Thanks for the RailsCast Ryan! I encourage people to check out the wiki as there are quite a bit of different ways to customize the behavior of the validations. (callbacks, filters, etc..)
Hi Brian,
I'm trying to get the demoed callbacks to work. Do I need to update
<%= javascript_include_tag :defaults, 'rails.validations', 'rails.validations.callbacks' %>
to specify the .callbacks JS file as well?I've updated my jquery-rails with the
--ui
tag yet, I'm getting 'easing' related errors in the jquery JS itself based on the example callback code.Thanks!
Mike.
Figured it out, solution is here.
Nice gem, nice presentation.
I would like to add a little suggestion. My watching experience would be even better if the lines in mate were wrapped, or broken up. As I often stop to read the code again - I am a fast learner if given plenty of time - I found it difficult to grasp the full context of the initializer file, line #9, stardate 2:10 to 2:20.
Thanks for the great work again.
In the screencast, you have *42* unread messages on github, Nice ! :)
Great ! I was playing with this gem since two weaks ago, trying to implement it in my project. However, my application raise`s an error that is making me mad: It`s raise an I18n::InvalidPluralizationData Exception. I`m using too delocalize gem, with helps me to translate my application to brazilian portuguese, and all my YML are working ok. Do you have any idea about this problem ? Thanks
Another option is to use jCheck: http://jcheck.net, it also has the jcheck-rails that does automatic validation generation.
I've been looking for an elegant way of doing this for a long while. Can't wait to checkout the code and take a look at how this is being implemented.
Nice screencast as always, looking forward to the redesign!
It would be even better if the textboxes do not lose the focus if a validation is invalid - but nice screencast as always, Thanks.
Great Cast and Plugin! One little thing: I don't think the confirm password message is so useful. It should appear on the confirm field, not on the password field. But that's not really an issue with the gem but with rails.
Great and thank you. I am trying to do some validation when exiting an input field. I changed one line in episode-136
In application.js line 9 change
this.submit(function() {
to:
this.blur(function() {
And get the following error:
Parameters: {"review"=>{"content"=>"some review"}}
ActionController::MethodNotAllowed (Only get, put, and delete requests are allowed.):
How can I get this to work with onBlur?
Oh, I would have posted to 136 but the signin using github did not work for me on that episode.
Thanks in advance!
Has anyone gotten this to work using form_tag instead of form_for? If so, would you please post an example? Thanks.
It will never work with the form_tag. Sorry.
Confused - when adding the client_side_validation plugin and updating the 'sign-up' form, Devise already has a 'form_for' method installed on this page. How do you get them both to work together as it keeps throwing up errors? Is daisy-chaining an option?
Sign Up
<%= form_for @users, :validate => true do |f| %>
Devise Plugin
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.label :email %>
<%= f.text_field :email %>
I take it you tried the following ?
This is what happened when I try to install it in Rails 3.0.7 and Ruby 1.9.1
$ rails g client_side_validations:install
/usr/local/lib/ruby/gems/1.9.1/gems/execjs-0.3.3/lib/execjs.rb:32:in
runtime': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeError)
module:ExecJS'from /usr/local/lib/ruby/gems/1.9.1/gems/execjs-0.3.3/lib/execjs.rb:44:in
from /usr/local/lib/ruby/gems/1.9.1/gems/execjs-0.3.3/lib/execjs.rb:3:in
<top (required)>'
require'from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in
from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in
<top (required)>'
require'from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in
from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in
<top (required)>'
require'from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/runtime.rb:68:in
from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/runtime.rb:68:in
block (2 levels) in require'
each'from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/runtime.rb:66:in
from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/runtime.rb:66:in
block in require'
each'from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/runtime.rb:55:in
from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler/runtime.rb:55:in
require'
require'from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.13/lib/bundler.rb:120:in
from /home/liboc/code-depot/qa-reg/TRAP/trunk/config/application.rb:7:in
<top (required)>'
require'from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:15:in
from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:15:in
<top (required)>'
require'from script/rails:6:in
from script/rails:6:in `'
Thanks Ryan! Great cast. But exists a version for rails 2.x? I have an Rails 2.x app, I cannot upgrade to 3 because of some main plugins still not available for rails 3.
I found this: https://github.com/dnclabs/client_side_validations But it seems outdated. Does anyone can help?
Thanks!
Martin
Rails 2.x is not supported in the new client_side_validations gem. The reason is because ActiveRecord 2.x does not allow reflection upon the validators. I solved this in DNC version of the gem by using a hack (validation-reflection gem) but supporting both Rails 2 & Rails 3 is very difficult so I decided to only support Rails 3.
Hello Ryan, really cool cast as always :)
I've got a field thats validated as
validates_length_of :badge_id, :is => 12, :message => "ID must be 12 digits", :unless => Proc.new{|r| r.badge_id == '0'}
which is causing the following error - this only goes away if I remove the:message => "..."
from the above validation:This column is a string and is also validated as
validates_numericality_of :badge_id, :only_integer => true
Thanks!,
Mike
Interestingly,
:unless => ...
overrides theis:
validation. I'm forced to go withvalidates_length_of :badge_id, :is => 12
The issue is it defaults to displaying
instead of
digits
. Hmm!At least this sheds some time on the conditional callback
:unless
. Any thoughts as to why:message => ...
isn't supported?Can you open an issue on Github? https://github.com/bcardarella/client_side_validations/issues
I liked the article so much, thanks you.
I found a problem when I try to use uniqueness validation. It requests an ajax request http://localhost:3000/validators/uniqueness?case_sensitive=true&book%5Bname%5D=test but i receive 404 Not Found.
Should I implement this request myself from rails side?
Same here, any luck solving the issue?
I read that this is a valid REST response. It's saying that "test" doesn't exist (the username is unique).
how did you solve your problem mahmoud?
Hi Ryan,
Thanks for the useful cast.
But i struck with it in my project. I used one of your cast to insert multiple tasks for a project through javascript, in that insert tasks by javascript you used date time to replace the child_index for form_for tag.
Here i got the validator objects but as the name of the fields changes it can't validate those.
Can you please help me in solving this problem.
Hi Ryan,
Not sure how to make the client side validation work in a form that uses mixed model fields i.e. user & account model
Any ideas ?
I'm using nested forms to which I add fields dynamically with ryans gem nested_form. How could these fields be validated?
Check out Ryan's nested form gem wiki.
https://github.com/ryanb/nested_form/wiki/Integrating-with-client_side_validations
Hope this could help you. I tested here and it was ok.
One of many thanks for your work. I however forgot to restart my server after making some of these changes... so took a while to figure my mistake. But, it works!
Did someone get this work under rails 3.1?
It's good, but I have some problems with graphics.
It worked fine on development but on production is does not work.
Have you managed to get it working in production? I'm having trouble getting it working in test & it's working fine in Producton!?
On using the above procedure, I get the following error message:
undefined method `error_message' for #ActionView::Helpers::FormBuilder:0x9fa7f54
Can anyone help me?? Thanks in advance..!!
Anyone using this with the jQuery datepicker? I haven't been able to get it to work - the form just never submits, even though there is a date selected.
Thanks for any help :)
So, the more I get into it, the more questions I have...
Also not having any luck getting it work with multiple checkboxes. Anyone have that setup working?
Thanks.
I ran this:
$ rails g client_side_validations:install
and got:
create config/initializers/client_side_validations.rb
however this file (create public/javascripts/rails.validations.js) didn't get created. is it because I'm in the new version of rails which has js in assets, and is this necessary for the inline validations, because mine aren't working. It works when I submit the form but not after I tab out of the text field. is there a way i can get this file to put in my assests.
thanks.
well i didn't initially see to add this //= require rails.validations in the application.js but i did and that didn't fix it either
I have the same issue. Tried to copy files from GitHub, but it didn't work for me.
does it work if you try and submit the form with blank fields?
I had the same issue. Here's how to solve it:
The //= require rails.validations
must be done before the //= require_tree
http://www.ddarrensmith.com/blog/2012/05/17/ruby-on-rails-client-side-validation-with-validation-helpers-and-twitter-bootstrap/
Can anyone tell me how to do the client side validation in dynamic text fields. How to validate for presence_of for the dynamic text field so that at least one dynamic text field must be present in the form.
Hi everyone...
How can I change the required/not required property or a given field ... based on the value of another completely different field?
Client_Side_Validations didn't work "out of the box" with simple_form for me.
Read this helpful post: http://www.ddarrensmith.com/blog/2012/05/17/ruby-on-rails-client-side-validation-with-validation-helpers-and-twitter-bootstrap/
And get this gist to make it work: https://gist.github.com/2705324#file_client_side_validations_twitter_boostrap_simple_form.js
They now work out of the box with bootstrap and simple-form if you use the simple-form plugin which supports bootstrap wrappers
The gems must go in this order
@Chris: Have you got it to work?
I have tried it (with simpleform and bootstrap) but doesn't work.
I have also tried the solution posted by Keil.
Any help would be appreciated.
Kind of cheating!
The gem is outdated for rails 4. Better try for an alternative
Can you tell us what an alternative would be?
bump :)
did someone found alternative for rails 4 and client side validation ?
client_side_validations isn't maintained anymore. Does anyone knows a alternative?
A new version of this gem was published recently enough . https://www.ruby-toolbox.com/projects/client_side_validations
I have found this alternative:
https://github.com/kalkov/rails4_client_side_validations
how can I use the client side validation with nested_
form_for
i am using client side side validation gem
Is this compatible with Rails4 ?
There's a branch that works for Rails 4.2, specify this on your Gemfile
gem 'client_side_validations', github: "DavyJonesLocker/client_side_validations", branch: "4-2-stable"
Be sure to check the Readme on the Github page, some things have changed a bit.