Permalink
Browse files

Merge pull request #369 from michaelbaldry/master

Do not validate fields with _destroy="1" in their input group
  • Loading branch information...
2 parents fe69111 + c8da755 commit 2a4d5f431378b15d741687c5fc6691b630736528 @bcardarella bcardarella committed Aug 16, 2012
@@ -86,41 +86,51 @@ validateForm = (form, validators) ->
validateElement = (element, validators) ->
element.trigger('element:validate:before')
-
- if element.data('changed') != false
+
+ passElement = ->
+ element.trigger('element:validate:pass').data('valid', null)
+
+ failElement = (message) ->
+ element.trigger('element:validate:fail', message).data('valid', false)
+ false
+
+ afterValidate = ->
+ element.trigger('element:validate:after').data('valid') != false
+
+ executeValidators = (context) ->
valid = true
- element.data('changed', false)
-
- # Because 'length' is defined on the list of validators we cannot call jQuery.each on
- context = ClientSideValidations.validators.local
+
for kind, fn of context
if validators[kind]
for validator in validators[kind]
if message = fn.call(context, element, validator)
- element.trigger('element:validate:fail', message).data('valid', false)
- valid = false
+ valid = failElement(message)
break
unless valid
break
+
+ valid
+
+
+ # if _destroy for this input group == "1" pass with flying colours, it'll get deleted anyway..
+ destroyInputName = element.attr('name').replace(/\[([^\]]*?)\]$/, '[_destroy]')
+ if $("input[name='#{destroyInputName}']").val() == "1"
+ passElement()
+ return afterValidate()
+
+ # if the value hasn't changed since last validation, do nothing
+ unless element.data('changed') != false
+ return afterValidate()
- if valid
- context = ClientSideValidations.validators.remote
- for kind, fn of context
- if validators[kind]
- for validator in validators[kind]
- if message = fn.call(context, element, validator)
- element.trigger('element:validate:fail', message).data('valid', false)
- valid = false
- break
- unless valid
- break
+ element.data('changed', false)
- if valid
- element.data('valid', null)
- element.trigger('element:validate:pass')
+ local = ClientSideValidations.validators.local
+ remote = ClientSideValidations.validators.remote
- element.trigger('element:validate:after')
- element.data('valid') != false
+ if executeValidators(local) and executeValidators(remote)
+ passElement()
+
+ afterValidate()
# Main hook
# If new forms are dynamically introduced into the DOM the .validate() method
@@ -61,7 +61,14 @@ module('Validate Element', {
id: 'user_phone_numbers_attributes_0_number',
'data-validate': 'true',
type: 'text'
- }))
+ })
+ .append($('<input />', {
+ name: 'user[phone_numbers_attributes][0][_destroy]',
+ id: 'user_phone_numbers_attributes_0__destroy',
+ 'data-validate': 'true',
+ type: 'hidden',
+ value: "1"
+ })))
.append($('<label for="user_phone_numbers_attributes_1_number">Phone Number</label>'))
.append($('<input />', {
name: 'user[phone_numbers_attributes][1][number]',
@@ -114,8 +121,8 @@ test('Validate nested attributes', function() {
input = form.find('input#user_phone_numbers_attributes_0_number');
label = $('label[for="user_phone_numbers_attributes_0_number"]');
input.trigger('focusout');
- ok(input.parent().hasClass('field_with_errors'));
- ok(label.parent().hasClass('field_with_errors'));
+ equal(input.parent().hasClass('field_with_errors'), false);
+ equal(label.parent().hasClass('field_with_errors'), false);
});
test('Validate when keyup on confirmation', function() {

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 2a4d5f4

Please sign in to comment.