Skip to content

Commit

Permalink
GRAILS-10871 - improve non domain class validation
Browse files Browse the repository at this point in the history
The validator should ignore properties which already have binding errors associated with them.
  • Loading branch information
Jeff Scott Brown committed Dec 3, 2013
1 parent b3a6b97 commit 8f797d1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ class ValidationSupport {
}
for (prop in constraints.values()) {
if (fieldsToValidate == null || fieldsToValidate.contains(prop.propertyName)) {
prop.messageSource = messageSource
prop.validate(object, object.getProperty(prop.propertyName), localErrors)
def fieldError = originalErrors.getFieldError(prop.propertyName)
if(fieldError == null || !fieldError.bindingFailure) {
prop.messageSource = messageSource
prop.validate(object, object.getProperty(prop.propertyName), localErrors)
}
}
}
object.errors = localErrors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package grails.validation

import grails.test.mixin.TestMixin
import grails.test.mixin.support.GrailsUnitTestMixin

import org.springframework.validation.FieldError

import spock.lang.Issue
import spock.lang.Specification

@TestMixin(GrailsUnitTestMixin)
Expand Down Expand Up @@ -77,6 +81,31 @@ class ValidateableSpec extends Specification {
!validateable.hasErrors()
validateable.errors.errorCount == 0
}

@Issue('GRAILS-10871')
void 'Test that binding failures are retained during validation and that the corresponding property is not validated'() {
given:
def validateable = new MyValidateable()

when:
def fieldError = new FieldError(MyValidateable.name, 'age', 'forty two', true, null, null, null)
validateable.errors.addError fieldError

then:
validateable.hasErrors()
validateable.errors.errorCount == 1
validateable.errors.getFieldError('age').rejectedValue == 'forty two'

when:
validateable.name = 'lower case'

then:
!validateable.validate()
validateable.hasErrors()
validateable.errors.errorCount == 2
validateable.errors.getFieldError('age').rejectedValue == 'forty two'
validateable.errors.getFieldError('name').rejectedValue == 'lower case'
}
}

@Validateable
Expand Down

0 comments on commit 8f797d1

Please sign in to comment.