Permalink
Browse files

Merge branch '2-0-stable'

  • Loading branch information...
2 parents 70fa35b + e6a62c1 commit b976da9063a01cc5fca686402211811f10c24d93 @bcardarella bcardarella committed Oct 9, 2012
@@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
gem.require_paths = ['lib']
gem.version = ClientSideValidations::SimpleForm::VERSION
- gem.add_dependency 'client_side_validations', '~> 3.2.0.rc.2'
+ gem.add_dependency 'client_side_validations', '~> 3.2.0'
gem.add_dependency 'simple_form', '~> 2.0.3'
gem.add_development_dependency 'rails', '~> 3.2.0'
@@ -31,7 +31,17 @@ ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] =
bootstrap:
add: (element, settings, message) ->
- @wrappers['default'].add.call(@, element, settings, message)
+ if element.data('valid') != false
+ wrapper_class_element = element.closest(".#{settings.wrapper_class}");
+ wrapper_tag_element = element.closest(settings.wrapper_tag)
+ wrapper_class_element.addClass(settings.wrapper_error_class)
+ errorElement = $("<#{settings.error_tag}/>", { class: settings.error_class, text: message })
+ wrapper_tag_element.append(errorElement)
+ else
+ element.parent().find("#{settings.error_tag}.#{settings.error_class}").text(message)
remove: (element, settings) ->
- @wrappers['default'].remove.call(@, element, settings)
-
+ wrapper_class_element = element.closest(".#{settings.wrapper_class}.#{settings.wrapper_error_class}")
+ wrapper_tag_element = element.closest(settings.wrapper_tag)
+ wrapper_class_element.removeClass(settings.wrapper_error_class)
+ errorElement = wrapper_tag_element.find("#{settings.error_tag}.#{settings.error_class}")
+ errorElement.remove()
@@ -1,5 +1,5 @@
module ClientSideValidations
module SimpleForm
- VERSION = '2.0.0.rc.2'
+ VERSION = '2.0.0'
end
end
@@ -0,0 +1,67 @@
+module('Validate SimpleForm Bootstrap', {
+ setup: function() {
+ window.ClientSideValidations.forms['new_user'] = {
+ type: 'SimpleForm::FormBuilder',
+ error_class: 'help-inline',
+ error_tag: 'span',
+ wrapper_error_class: 'error',
+ wrapper_tag: 'div',
+ wrapper_class: 'control-group',
+ wrapper: 'bootstrap',
+ validators: {
+ "user[name]":{"presence":[{"message": "must be present"}], "format":[{"message":"is invalid","with":/\d+/}]}
+ }
+ }
+
+ $('#qunit-fixture')
+ .append($('<form />', {
+ action: '/users',
+ 'data-validate': true,
+ method: 'post',
+ id: 'new_user'
+ }))
+ .find('form')
+ .append($('<div />', {
+ class: 'form-inputs'
+ }))
+ .find('div')
+ .append($('<div />', {
+ class: 'control-group'
+ })).find('div')
+ .append($('<label for="user_name" class="string control-label">Name</label>'))
+ .append($('<div />', {
+ class: 'controls'
+ })).find('div')
+ .append($('<input />', {
+ name: 'user[name]',
+ id: 'user_name',
+ type: 'text'
+ }));
+
+ $('form#new_user').validate();
+ }
+});
+
+test('Validate error attaching and detaching', function() {
+ var form = $('form#new_user'), input = form.find('input#user_name');
+ var label = $('label[for="user_name"]');
+
+ input.trigger('focusout');
+ ok(input.parent().parent().hasClass('error'));
+ ok(label.parent().hasClass('error'));
+ ok(input.parent().find('span.help-inline:contains("must be present")')[0]);
+
+ input.val('abc')
+ input.trigger('change')
+ input.trigger('focusout')
+ ok(input.parent().parent().hasClass('error'));
+ ok(label.parent().hasClass('error'));
+ ok(input.parent().find('span.help-inline:contains("is invalid")')[0]);
+
+ input.val('123')
+ input.trigger('change')
+ input.trigger('focusout')
+ ok(!input.parent().parent().hasClass('error'));
+ ok(!label.parent().hasClass('error'));
+ ok(!input.parent().find('span.help-inline')[0]);
+});
@@ -1,6 +1,6 @@
/*
- Client Side Validations - SimpleForm - v2.0.0.rc.1
+ Client Side Validations - SimpleForm - v2.0.0.rc.2
https://github.com/dockyard/client_side_validations-simple_form
Copyright (c) 2012 DockYard, LLC
@@ -44,10 +44,27 @@
},
bootstrap: {
add: function(element, settings, message) {
- return this.wrappers['default'].add.call(this, element, settings, message);
+ var errorElement, wrapper_class_element, wrapper_tag_element;
+ if (element.data('valid') !== false) {
+ wrapper_class_element = element.closest("." + settings.wrapper_class);
+ wrapper_tag_element = element.closest(settings.wrapper_tag);
+ wrapper_class_element.addClass(settings.wrapper_error_class);
+ errorElement = $("<" + settings.error_tag + "/>", {
+ "class": settings.error_class,
+ text: message
+ });
+ return wrapper_tag_element.append(errorElement);
+ } else {
+ return element.parent().find("" + settings.error_tag + "." + settings.error_class).text(message);
+ }
},
remove: function(element, settings) {
- return this.wrappers['default'].remove.call(this, element, settings);
+ var errorElement, wrapper_class_element, wrapper_tag_element;
+ wrapper_class_element = element.closest("." + settings.wrapper_class + "." + settings.wrapper_error_class);
+ wrapper_tag_element = element.closest(settings.wrapper_tag);
+ wrapper_class_element.removeClass(settings.wrapper_error_class);
+ errorElement = wrapper_tag_element.find("" + settings.error_tag + "." + settings.error_class);
+ return errorElement.remove();
}
}
}

0 comments on commit b976da9

Please sign in to comment.