Skip to content

Commit d162c08

Browse files
author
graeme
committed
fix for GRAILS-2656
git-svn-id: https://svn.codehaus.org/grails/trunk@6816 1cfb16fd-6d17-0410-8ff1-b7e8e1e2867d
1 parent 67719fb commit d162c08

File tree

5 files changed

+118
-14
lines changed

5 files changed

+118
-14
lines changed

src/commons/org/codehaus/groovy/grails/validation/BlankConstraint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ protected boolean processValidateWithVetoing(Object target, Object propertyValue
6464
if(!blank) {
6565
Object[] args = new Object[] { constraintPropertyName, constraintOwningClass };
6666
super.rejectValue( target,errors, ConstrainedProperty.DEFAULT_BLANK_MESSAGE_CODE, ConstrainedProperty.BLANK_CONSTRAINT, args );
67+
// empty string is catched by 'blank' constraint, no addition validation needed
68+
return true;
6769
}
68-
// empty string is catched by 'blank' constraint, no addition validation needed
69-
return true;
7070
}
7171
return false;
7272
}

src/commons/org/codehaus/groovy/grails/validation/NullableConstraint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ protected boolean processValidateWithVetoing(Object target, Object propertyValue
6363
if(!nullable) {
6464
Object[] args = new Object[] { constraintPropertyName, constraintOwningClass};
6565
super.rejectValue(target, errors, ConstrainedProperty.DEFAULT_NULL_MESSAGE_CODE, ConstrainedProperty.NULLABLE_CONSTRAINT,args );
66+
// null value is catched by 'blank' constraint, no addition validation needed
67+
return true;
6668
}
67-
// null value is catched by 'blank' constraint, no addition validation needed
68-
return true;
6969
}
7070
return false;
7171
}

test/commons/org/codehaus/groovy/grails/validation/BlankConstraintTests.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ public void testValidate() {
2323
"someData"
2424
);
2525

26-
testConstraintPassedAndVetoed(
27-
getConstraint( "testString", Boolean.TRUE ),
28-
""
29-
);
30-
3126
testConstraintPassed(
3227
getConstraint( "testString", Boolean.TRUE ),
3328
"someData"

test/commons/org/codehaus/groovy/grails/validation/NullableConstraint2Tests.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ public void testValidation() {
2323
"test"
2424
);
2525

26-
testConstraintPassedAndVetoed(
27-
getConstraint( "testString", Boolean.TRUE ),
28-
null
29-
);
30-
3126
testConstraintPassed(
3227
getConstraint( "testString", Boolean.TRUE ),
3328
""
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package org.codehaus.groovy.grails.validation
2+
3+
import org.codehaus.groovy.grails.orm.hibernate.AbstractGrailsHibernateTests
4+
import org.springframework.validation.BindingResult
5+
6+
/**
7+
* @author Graeme Rocher
8+
* @since 1.0
9+
*
10+
* Created: Mar 20, 2008
11+
*/
12+
class VetoingNullableBehaviourTests extends AbstractGrailsHibernateTests{
13+
14+
protected void onSetUp() {
15+
gcl.parseClass('''
16+
import org.apache.commons.lang.StringUtils
17+
18+
class VetoingNullableBehaviour {
19+
Long id
20+
Long version
21+
22+
String name
23+
24+
static constraints = {
25+
name(nullable:true, validator:{ val ->
26+
if(val) {
27+
return Character.isUpperCase(val.toCharArray()[0])
28+
}
29+
})
30+
}
31+
}
32+
33+
34+
class VetoingNullableBehaviourBook {
35+
Long id
36+
Long version
37+
Boolean online
38+
String onlineFormatDescription
39+
40+
/*
41+
Premise here is that onlineFormatDescription is only required IF online == true
42+
*/
43+
static constraints = {
44+
online(nullable: false)
45+
onlineFormatDescription(nullable: true, blank: true, validator: onlineValidator)
46+
}
47+
48+
static onlineValidator = {val, obj ->
49+
if (obj.properties['online'] == true && StringUtils.isBlank(val)) {
50+
return ['blank']
51+
}
52+
}
53+
54+
}
55+
56+
''')
57+
}
58+
59+
60+
61+
void testVetoingConstraint() {
62+
def test = ga.getDomainClass("VetoingNullableBehaviour").newInstance()
63+
64+
65+
66+
assert test.validate()
67+
68+
test.name = "fred"
69+
70+
assert !test.validate()
71+
72+
test.name = "Fred"
73+
74+
assert test.validate()
75+
}
76+
77+
//this test will fail, but should pass. it never gets to the custom validator because the nullable constraint stops further validation
78+
void testOnlineFormatDescriptionValidates_null() {
79+
def book = ga.getDomainClass("VetoingNullableBehaviourBook").newInstance()
80+
book.online = true
81+
book.onlineFormatDescription = null
82+
83+
book.validate()
84+
assertFieldHasError(book.errors, 'onlineFormatDescription')
85+
}
86+
87+
//this test will fail, but should pass. it never gets to the custom validator because the blank constraint stops further validation
88+
void testOnlineFormatDescriptionValidates_blank() {
89+
def book = ga.getDomainClass("VetoingNullableBehaviourBook").newInstance()
90+
book.online = true
91+
book.onlineFormatDescription = ''
92+
93+
book.validate()
94+
assertFieldHasError(book.errors, 'onlineFormatDescription')
95+
}
96+
97+
//this test will pass
98+
void testOnlineFormatDescriptionValidates_valid() {
99+
def book = ga.getDomainClass("VetoingNullableBehaviourBook").newInstance()
100+
book.online = true
101+
book.onlineFormatDescription = 'Title'
102+
book.validate()
103+
assertFieldDoesNotHasError(book.errors, 'onlineFormatDescription')
104+
}
105+
106+
107+
protected assertFieldHasError(BindingResult bindErrors, String field) {
108+
assertTrue("Error not found for field ${field}, errors were: ${bindErrors}", bindErrors.getFieldError(field) != null)
109+
}
110+
111+
protected assertFieldDoesNotHasError(BindingResult bindErrors, String field) {
112+
assertTrue("Error not found for field ${field}, errors were: ${bindErrors}", bindErrors.getFieldError(field) == null)
113+
}
114+
}

0 commit comments

Comments
 (0)