Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Test + Bug fix (CS) for multiple field error presentation on submit #259

Closed
wants to merge 6 commits into from

4 participants

@pacovell

...t returning assignment value + jQuery exit on .each() function returning false

@bcardarella
Owner

Very sorry I let this go, can you rebase against master if this is still relevant?

@pacovell

Brian, no longer sure exactly which commits you want -- all of them, or just the one from me?

@bcardarella
Owner

@paulcovell I believe someone else actually tackled this. I'm going to close, sorry about the delay, thank you for getting back

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 26, 2012
  1. @pacovell

    Prevent premature exit on validation checking loop caused by Coffeesc…

    pacovell authored
    …ript returning assignment value + jQuery exit on .each() function returning false
Commits on Jan 30, 2012
  1. @pacovell
Commits on Feb 24, 2012
  1. @ivanoats @pacovell
  2. @ivanoats @pacovell

    assets plural

    ivanoats authored pacovell committed
  3. @carlosramireziii @pacovell
  4. @carlosramireziii @pacovell
This page is out of date. Refresh to see the latest.
View
6 README.md
@@ -44,6 +44,12 @@ This will install the initializer:
config/initializers/client_side_validations.rb
```
+If you are using Rails 3.1+ you'll need to use:
+
+```
+rails g client_side_validations:copy_assets
+```
+
## Usage ##
The javascript file is served up in the asset pipeline. Add the
View
1  coffeescript/rails.validations.coffee
@@ -68,6 +68,7 @@ validateForm = (form, validators) ->
valid = true
form.find('[data-validate="true"]:input:enabled').each ->
valid = false if $(@).isValid(validators)
+ return undefined
if valid then form.trigger('form:validate:pass') else form.trigger('form:validate:fail')
View
18 lib/client_side_validations/action_view/form_helper.rb
@@ -75,11 +75,23 @@ def client_side_form_settings(object, options)
if options[:html] && options[:html][:id]
var_name = options[:html][:id]
else
- var_name = if object.respond_to?(:persisted?) && object.persisted?
- options[:as] ? "#{options[:as]}_edit" : dom_id(object, :edit)
+ if Rails.version >= '3.2.0'
+ var_name = if object.respond_to?(:persisted?) && object.persisted?
+ options[:as] ? "edit_#{options[:as]}" : [options[:namespace], dom_id(object, :edit)].compact.join("_")
+ else
+ options[:as] ? "new_#{options[:as]}" : [options[:namespace], dom_id(object)].compact.join("_")
+ end
else
- options[:as] ? "#{options[:as]}_new" : dom_id(object)
+ # This is to maintain backward compatibility with Rails 3.1
+ # see: https://github.com/rails/rails/commit/e29773f885fd500189ffd964550ae20061d745ba#commitcomment-948052
+ var_name = if object.respond_to?(:persisted?) && object.persisted?
+ options[:as] ? "#{options[:as]}_edit" : dom_id(object, :edit)
+ else
+ options[:as] ? "#{options[:as]}_new" : dom_id(object)
+ end
end
+
+
end
content_tag(:script) do
View
2  test/action_view/cases/helper.rb
@@ -169,7 +169,7 @@ def admin_comment_path(post, comment)
end
alias_method :admin_post_comment_path, :admin_comment_path
- def posts_path
+ def posts_path(options={})
"/posts"
end
View
66 test/action_view/cases/test_helpers.rb
@@ -640,5 +640,71 @@ def test_pushing_script_to_content_for
assert_equal expected, output_buffer
assert_equal build_script_tag(nil, "edit_post_123", validators), content_for(:post)
end
+
+ def test_as_form_option_with_new_record_rails_3_2
+ skip("This test is only applicable for Rails ~> v3.2.0") unless Rails.version >= '3.2.0'
+ test_buffer = form_for(Post.new, :as => :article, :validate => true) do |f|
+ concat content_tag(:span, "Dummy Content")
+ end
+ expected = whole_form("/posts", "new_article", "new_article", :validators => {}) do
+ %{<span>Dummy Content</span>}
+ end
+ assert_equal expected, output_buffer
+ end
+
+ def test_as_form_option_with_existing_record_rails_3_2
+ skip("This test is only applicable for Rails ~> v3.2.0") unless Rails.version >= '3.2.0'
+ test_buffer = form_for(@post, :as => :article, :validate => true) do |f|
+ concat content_tag(:span, "Dummy Content")
+ end
+ expected = whole_form("/posts/123", "edit_article", "edit_article", :method => "put", :validators => {}) do
+ %{<span>Dummy Content</span>}
+ end
+ assert_equal expected, output_buffer
+ end
+
+ def test_as_form_option_with_new_record_rails_3_1
+ skip("This test is only applicable for Rails ~> v3.1.0") if Rails.version >= '3.2.0'
+ test_buffer = form_for(Post.new, :as => :article, :validate => true) do |f|
+ concat content_tag(:span, "Dummy Content")
+ end
+ expected = whole_form("/posts", "article_new", "article_new", :validators => {}) do
+ %{<span>Dummy Content</span>}
+ end
+ assert_equal expected, output_buffer
+ end
+
+ def test_as_form_option_with_existing_record_rails_3_1
+ skip("This test is only applicable for Rails ~> v3.1.0") if Rails.version >= '3.2.0'
+ test_buffer = form_for(@post, :as => :article, :validate => true) do |f|
+ concat content_tag(:span, "Dummy Content")
+ end
+ expected = whole_form("/posts/123", "article_edit", "article_edit", :method => "put", :validators => {}) do
+ %{<span>Dummy Content</span>}
+ end
+ assert_equal expected, output_buffer
+ end
+
+ def test_namespace_form_option_with_new_record
+ skip("This test is only applicable for Rails ~> v3.2.0") unless Rails.version >= '3.2.0'
+ test_buffer = form_for(Post.new, :namespace => :blog, :validate => true) do |f|
+ concat content_tag(:span, "Dummy Content")
+ end
+ expected = whole_form("/posts", "blog_new_post", "new_post", :validators => {}) do
+ %{<span>Dummy Content</span>}
+ end
+ assert_equal expected, output_buffer
+ end
+
+ def test_namespace_form_option_with_existing_record
+ skip("This test is only applicable for Rails ~> v3.2.0") unless Rails.version >= '3.2.0'
+ test_buffer = form_for(@post, :namespace => :blog, :validate => true) do |f|
+ concat content_tag(:span, "Dummy Content")
+ end
+ expected = whole_form("/posts/123", "blog_edit_post_123", "edit_post", :method => "put", :validators => {}) do
+ %{<span>Dummy Content</span>}
+ end
+ assert_equal expected, output_buffer
+ end
end
View
33 test/javascript/public/test/form_builders/validateForm.js
@@ -64,6 +64,38 @@ asyncTest('Validate form with an input changed to false', 1, function() {
}, 30);
});
+asyncTest('Validate form should hit multiple inputs', 5, function() {
+ $('#qunit-fixture')
+ .find('form')
+ .append($('<input />', {
+ name: 'user[address]',
+ id: 'user_address',
+ 'data-validate': 'true',
+ type: 'text'
+ }))
+ .append($('<label for="user_address">Address</label>'));
+
+ ClientSideValidations.forms['new_user'].validators['user[address]'] = {
+ 'presence':{'message':'must be present'}
+ }
+
+ var form = $('form#new_user'),
+ input1 = form.find('input#user_name'),
+ input2 = form.find('input#user_address');
+
+ $('form#new_user').validate();
+
+ form.trigger('submit');
+ setTimeout(function() {
+ start();
+ ok(input1.parent().hasClass('field_with_errors'));
+ ok(input2.parent().hasClass('field_with_errors'));
+ ok(input1.parent().find('label:contains("must be present")')[0]);
+ ok(input2.parent().find('label:contains("must be present")')[0]);
+ ok(!$('iframe').contents().find('p:contains("Form submitted")')[0]);
+ }, 30);
+});
+
asyncTest('Ensure ajax:beforeSend is not from a bubbled event', 1, function() {
var form = $('form#new_user'), input = form.find('input#user_name');
@@ -75,3 +107,4 @@ asyncTest('Ensure ajax:beforeSend is not from a bubbled event', 1, function() {
ok(!input.parent().hasClass('field_with_errors'));
});
});
+
View
12 vendor/assets/javascripts/rails.validations.js
@@ -1,13 +1,3 @@
-
-/*
- Rails 3 Client Side Validations - v3.2.0.beta.2
- https://github.com/bcardarella/client_side_validations
-
- Copyright (c) 2012 Brian Cardarella
- Licensed under the MIT license
- http://www.opensource.org/licenses/mit-license.php
-*/
-
(function() {
var $, validateElement, validateForm,
__indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
@@ -123,7 +113,7 @@
form.trigger('form:validate:before');
valid = true;
form.find('[data-validate="true"]:input:enabled').each(function() {
- if ($(this).isValid(validators)) return valid = false;
+ if ($(this).isValid(validators)) valid = false;
});
if (valid) {
form.trigger('form:validate:pass');
Something went wrong with that request. Please try again.