Permalink
Browse files

Handle exisitng errors from server-side

  • Loading branch information...
1 parent b294ab7 commit 45a310e91141a3f8bd284690d45ee91cf5442e58 @bcardarella bcardarella committed Nov 19, 2012
@@ -16,13 +16,13 @@ ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] =
wrappers:
default:
add: (element, settings, message) ->
- if element.data('valid') != false
- wrapper = element.closest(settings.wrapper_tag)
- wrapper.addClass(settings.wrapper_error_class)
+ errorElement = element.parent().find "#{settings.error_tag}.#{settings.error_class}"
+ wrapper = element.closest(settings.wrapper_tag)
+ if not errorElement[0]?
errorElement = $("<#{settings.error_tag}/>", { class: settings.error_class, text: message })
wrapper.append(errorElement)
- else
- element.parent().find("#{settings.error_tag}.#{settings.error_class}").text(message)
+ wrapper.addClass(settings.wrapper_error_class)
+ errorElement.text(message)
remove: (element, settings) ->
wrapper = element.closest("#{settings.wrapper_tag}.#{settings.wrapper_error_class}")
wrapper.removeClass(settings.wrapper_error_class)
@@ -38,7 +38,7 @@ ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] =
wrapper_tag_element.append(errorElement)
wrapper_class_element = element.closest(".#{settings.wrapper_class}");
wrapper_class_element.addClass(settings.wrapper_error_class)
- errorElement.text message
+ errorElement.text(message)
remove: (element, settings) ->
wrapper_class_element = element.closest(".#{settings.wrapper_class}.#{settings.wrapper_error_class}")
wrapper_tag_element = element.closest(settings.wrapper_tag)
@@ -56,3 +56,16 @@ test('Validate error attaching and detaching', function() {
ok(!input.parent().find('span.error')[0]);
});
+test('Validate pre-existing error blocks are re-used', function() {
+ var form = $('form#new_user'), input = form.find('input#user_name');
+ var label = $('label[for="user_name"]');
+
+ input.parent().append($('<span class="error">Error from Server</span>'))
+ ok(input.parent().find('span.error:contains("Error from Server")')[0]);
+ input.val('abc')
+ input.trigger('change')
+ input.trigger('focusout')
+ ok(input.parent().hasClass('field_with_errors'));
+ ok(label.parent().hasClass('field_with_errors'));
+ ok(input.parent().find('span.error:contains("is invalid")').size() === 1);
+});
@@ -66,7 +66,6 @@ test('Validate error attaching and detaching', function() {
ok(!input.parent().find('span.help-inline')[0]);
});
-
test('Validate pre-existing error blocks are re-used', function() {
var form = $('form#new_user'), input = form.find('input#user_name');
var label = $('label[for="user_name"]');
@@ -22,17 +22,17 @@
"default": {
add: function(element, settings, message) {
var errorElement, wrapper;
- if (element.data('valid') !== false) {
- wrapper = element.closest(settings.wrapper_tag);
- wrapper.addClass(settings.wrapper_error_class);
+ errorElement = element.parent().find("" + settings.error_tag + "." + settings.error_class);
+ wrapper = element.closest(settings.wrapper_tag);
+ if (!(errorElement[0] != null)) {
errorElement = $("<" + settings.error_tag + "/>", {
"class": settings.error_class,
text: message
});
- return wrapper.append(errorElement);
- } else {
- return element.parent().find("" + settings.error_tag + "." + settings.error_class).text(message);
+ wrapper.append(errorElement);
}
+ wrapper.addClass(settings.wrapper_error_class);
+ return errorElement.text(message);
},
remove: function(element, settings) {
var errorElement, wrapper;

0 comments on commit 45a310e

Please sign in to comment.