Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions coffeescript/rails.validations.coffee
Expand Up @@ -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')

Expand Down
18 changes: 15 additions & 3 deletions lib/client_side_validations/action_view/form_helper.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/action_view/cases/helper.rb
Expand Up @@ -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

Expand Down
66 changes: 66 additions & 0 deletions test/action_view/cases/test_helpers.rb
Expand Up @@ -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

33 changes: 33 additions & 0 deletions test/javascript/public/test/form_builders/validateForm.js
Expand Up @@ -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');

Expand All @@ -75,3 +107,4 @@ asyncTest('Ensure ajax:beforeSend is not from a bubbled event', 1, function() {
ok(!input.parent().hasClass('field_with_errors'));
});
});

12 changes: 1 addition & 11 deletions vendor/assets/javascripts/rails.validations.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.