GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / commons / org / codehaus / groovy / grails / validation / MatchesConstraint.java
blobc23a1d6779938e720717426ab93e23048feb9d84
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 the property against a supplied regular expression
22 * @author Graeme Rocher
23 * @since 0.4
24 * <p/>
25 * Created: Jan 19, 2007
26 * Time: 8:22:46 AM
29 class MatchesConstraint extends AbstractConstraint {
31 private String regex;
33 /**
34 * @return Returns the regex.
36 public String getRegex() {
37 return regex;
40 /* (non-Javadoc)
41 * @see org.codehaus.groovy.grails.validation.Constraint#supports(java.lang.Class)
43 public boolean supports(Class type) {
44 return type != null && String.class.isAssignableFrom(type);
48 /* (non-Javadoc)
49 * @see org.codehaus.groovy.grails.validation.ConstrainedProperty.AbstractConstraint#setParameter(java.lang.Object)
51 public void setParameter(Object constraintParameter) {
52 if(!(constraintParameter instanceof String))
53 throw new IllegalArgumentException("Parameter for constraint ["+ConstrainedProperty.MATCHES_CONSTRAINT+"] of property ["+constraintPropertyName+"] of class ["+constraintOwningClass+"] must be of type [java.lang.String]");
55 this.regex = (String)constraintParameter;
56 super.setParameter(constraintParameter);
59 public String getName() {
60 return ConstrainedProperty.MATCHES_CONSTRAINT;
63 protected void processValidate(Object target, Object propertyValue, Errors errors) {
64 if(!propertyValue.toString().matches( regex )) {
65 Object[] args = new Object[] { constraintPropertyName, constraintOwningClass, propertyValue, regex };
66 super.rejectValue(target,errors,ConstrainedProperty.DEFAULT_DOESNT_MATCH_MESSAGE_CODE,ConstrainedProperty.MATCHES_CONSTRAINT + ConstrainedProperty.INVALID_SUFFIX,args);