Skip to content

Commit 8f797d1

Browse files
GRAILS-10871 - improve non domain class validation
The validator should ignore properties which already have binding errors associated with them.
1 parent b3a6b97 commit 8f797d1

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

grails-plugin-validation/src/main/groovy/org/codehaus/groovy/grails/web/plugins/support/ValidationSupport.groovy

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ class ValidationSupport {
5656
}
5757
for (prop in constraints.values()) {
5858
if (fieldsToValidate == null || fieldsToValidate.contains(prop.propertyName)) {
59-
prop.messageSource = messageSource
60-
prop.validate(object, object.getProperty(prop.propertyName), localErrors)
59+
def fieldError = originalErrors.getFieldError(prop.propertyName)
60+
if(fieldError == null || !fieldError.bindingFailure) {
61+
prop.messageSource = messageSource
62+
prop.validate(object, object.getProperty(prop.propertyName), localErrors)
63+
}
6164
}
6265
}
6366
object.errors = localErrors

grails-test-suite-uber/src/test/groovy/grails/validation/ValidateableSpec.groovy

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package grails.validation
22

33
import grails.test.mixin.TestMixin
44
import grails.test.mixin.support.GrailsUnitTestMixin
5+
6+
import org.springframework.validation.FieldError
7+
8+
import spock.lang.Issue
59
import spock.lang.Specification
610

711
@TestMixin(GrailsUnitTestMixin)
@@ -77,6 +81,31 @@ class ValidateableSpec extends Specification {
7781
!validateable.hasErrors()
7882
validateable.errors.errorCount == 0
7983
}
84+
85+
@Issue('GRAILS-10871')
86+
void 'Test that binding failures are retained during validation and that the corresponding property is not validated'() {
87+
given:
88+
def validateable = new MyValidateable()
89+
90+
when:
91+
def fieldError = new FieldError(MyValidateable.name, 'age', 'forty two', true, null, null, null)
92+
validateable.errors.addError fieldError
93+
94+
then:
95+
validateable.hasErrors()
96+
validateable.errors.errorCount == 1
97+
validateable.errors.getFieldError('age').rejectedValue == 'forty two'
98+
99+
when:
100+
validateable.name = 'lower case'
101+
102+
then:
103+
!validateable.validate()
104+
validateable.hasErrors()
105+
validateable.errors.errorCount == 2
106+
validateable.errors.getFieldError('age').rejectedValue == 'forty two'
107+
validateable.errors.getFieldError('name').rejectedValue == 'lower case'
108+
}
80109
}
81110

82111
@Validateable

0 commit comments

Comments
 (0)