GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / commons / org / codehaus / groovy / grails / validation / NullableConstraint.java
blobb0a4e65369eef82f247c0cdc3e48e31ac0e275ba
1 /* Copyright 2004-2005 Graeme Rocher
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 package org.codehaus.groovy.grails.validation;
17 import org.springframework.validation.Errors;
19 /**
20 * A Constraint that validates not equal to something
22 * @author Graeme Rocher
23 * @author Sergey Nebolsin
24 * @since 0.4
26 class NullableConstraint extends AbstractVetoingConstraint {
28 private boolean nullable;
31 public boolean isNullable() {
32 return nullable;
35 /* (non-Javadoc)
36 * @see org.codehaus.groovy.grails.validation.Constraint#supports(java.lang.Class)
38 public boolean supports(Class type) {
39 return type != null && !type.isPrimitive();
42 /* (non-Javadoc)
43 * @see org.codehaus.groovy.grails.validation.ConstrainedProperty.AbstractConstraint#setParameter(java.lang.Object)
45 public void setParameter(Object constraintParameter) {
46 if(!(constraintParameter instanceof Boolean))
47 throw new IllegalArgumentException("Parameter for constraint ["+ConstrainedProperty.NULLABLE_CONSTRAINT+"] of property ["+constraintPropertyName+"] of class ["+constraintOwningClass+"] must be a boolean value");
49 this.nullable = ((Boolean)constraintParameter).booleanValue();
50 super.setParameter(constraintParameter);
53 public String getName() {
54 return ConstrainedProperty.NULLABLE_CONSTRAINT;
57 protected boolean skipNullValues() {
58 return false;
61 protected boolean processValidateWithVetoing(Object target, Object propertyValue, Errors errors) {
62 if(propertyValue == null) {
63 if(!nullable) {
64 Object[] args = new Object[] { constraintPropertyName, constraintOwningClass};
65 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;
70 return false;