1
1
package com.cscinfo.platform.constraint
2
2
3
- import org.grails.datastore.gorm.validation.constraints.AbstractVetoingConstraint
3
+ import groovy.transform.CompileDynamic
4
+ import groovy.transform.CompileStatic
5
+ import org.grails.datastore.gorm.validation.constraints.AbstractConstraint
4
6
import org.springframework.context.MessageSource
5
7
import org.springframework.validation.Errors
6
8
import org.springframework.validation.FieldError
@@ -16,67 +18,84 @@ import org.springframework.validation.FieldError
16
18
* @author Eric Kelm
17
19
* @author Russell Morrisey
18
20
*/
19
- class CascadeValidationConstraint extends AbstractVetoingConstraint {
20
- public static final String NAME = " cascadeValidate"
21
+ @CompileStatic
22
+ class CascadeConstraint extends AbstractConstraint {
23
+ static final String CASCADE_CONSTRAINT = " cascade"
21
24
22
- private final boolean cascade
23
-
24
- CascadeValidationConstraint (Class<?> constraintOwningClass , String constraintPropertyName , Object constraintParameter , MessageSource messageSource ) {
25
+ CascadeConstraint (Class<?> constraintOwningClass , String constraintPropertyName , Object constraintParameter , MessageSource messageSource ) {
25
26
super (constraintOwningClass, constraintPropertyName, constraintParameter, messageSource)
26
- this . cascade = (Boolean ) constraintParameter
27
+
28
+ if (! (constraintParameter instanceof Boolean )) {
29
+ throw new IllegalArgumentException (" Parameter for constraint [$CASCADE_CONSTRAINT ] of property [$constraintPropertyName ] of class [$constraintOwningClass ] must be a boolean" )
30
+ }
27
31
}
28
32
29
- String getName () { NAME }
33
+ boolean supports (Class type ) {
34
+ Collection . isAssignableFrom(type) || type. metaClass. respondsTo(type, ' validate' )
35
+ }
36
+
37
+ String getName () {
38
+ return CASCADE_CONSTRAINT
39
+ }
40
+
41
+ protected void processValidate (Object target , Object propertyValue , Errors errors ) {
30
42
31
- @Override
32
- protected boolean processValidateWithVetoing (target , propertyValue , Errors errors ) {
33
43
boolean result = false
34
44
35
45
if (propertyValue instanceof Collection ) {
36
46
propertyValue. eachWithIndex { item , pvIdx ->
37
- result = validateValue(target, item, errors, pvIdx) || result
47
+ validateValue(target, item, errors, pvIdx) || result
38
48
}
39
49
} else {
40
- result = validateValue(target, propertyValue, errors)
50
+ validateValue(target, propertyValue, errors)
41
51
}
42
-
43
- return result
44
52
}
45
53
46
- private boolean validateValue (target , value , errors , index = null ) {
54
+ /**
55
+ * Processes the validation of the propertyValue, against the checks patterns set, and setting and calling rejectValue
56
+ * if the propertyValue matches any of the patterns in the checks list.
57
+ *
58
+ * @param target The target field to verify.
59
+ * @param propertyValue the property value of the field.
60
+ * @param errors Errors to be sent by rejectValues,.
61
+ */
62
+ @CompileDynamic
63
+ private void validateValue (target , value , errors , index = null ) {
47
64
if (! value. respondsTo(' validate' )) {
48
65
throw new NoSuchMethodException (" Error validating field [${ constraintPropertyName} ]. Unable to apply 'cascade' constraint on [${ value.class} ] because the object does not have a validate() method. If the object is a command object, you may need to add the @Validateable annotation to the class definition." )
49
66
}
50
67
68
+ if (! getParameter()) {
69
+ return
70
+ }
71
+
51
72
if (value. validate()) {
52
- return false
73
+ return
53
74
}
54
75
55
76
String objectName = target. errors. objectName
56
77
Errors childErrors = value. errors
57
78
List<FieldError > childFieldErrors = childErrors. fieldErrors
79
+
58
80
childFieldErrors. each { FieldError childFieldError ->
59
81
String field
60
- if (index != null ) {
82
+
83
+ if (index != null ) {
61
84
field = " ${ propertyName} .${ index} .${ childFieldError.field} "
62
85
} else {
63
86
field = " ${ propertyName} .${ childFieldError.field} "
64
87
}
88
+
65
89
FieldError fieldError = new FieldError (objectName, field, childFieldError. rejectedValue, childFieldError. bindingFailure, childFieldError. codes, childFieldError. arguments, childFieldError. defaultMessage)
66
90
errors. addError(fieldError)
67
91
}
68
- return true
69
- }
70
-
71
- boolean supports (Class type ) {
72
- Collection . isAssignableFrom(type) || type. metaClass. respondsTo(type, ' validate' )
73
92
}
74
93
75
94
@Override
76
95
protected Object validateParameter (Object constraintParameter ) {
77
96
if (! (constraintParameter instanceof Boolean )) {
78
97
throw new IllegalArgumentException (" Parameter for constraint [" +
79
- NAME + " ] of property [" +
98
+ CASCADE_CONSTRAINT + " ] of property [" +
80
99
constraintPropertyName + " ] of class [" + constraintOwningClass +
81
100
" ] must be a boolean value" )
82
101
}
0 commit comments