GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / commons / org / codehaus / groovy / grails / validation / CreditCardConstraint.java
blob8a9e90f4d2bcd1a57a9114084c3c1332b9a8871e
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;
18 import org.apache.commons.validator.CreditCardValidator;
20 /**
21 * A constraint class that validates a credit card number
23 * @author Graeme Rocher
24 * @since 0.4
25 * <p/>
26 * Created: Jan 19, 2007
27 * Time: 8:12:04 AM
29 class CreditCardConstraint extends AbstractConstraint {
30 private boolean creditCard;
33 protected void processValidate(Object target, Object propertyValue, Errors errors) {
34 if(creditCard) {
35 CreditCardValidator validator = new CreditCardValidator();
37 if(!validator.isValid(propertyValue.toString()) ) {
38 Object[] args = new Object[] { constraintPropertyName, constraintOwningClass, propertyValue };
39 super.rejectValue(target,errors,ConstrainedProperty.DEFAULT_INVALID_CREDIT_CARD_MESSAGE_CODE,ConstrainedProperty.CREDIT_CARD_CONSTRAINT + ConstrainedProperty.INVALID_SUFFIX,args);
44 public void setParameter(Object constraintParameter) {
45 if(!(constraintParameter instanceof Boolean))
46 throw new IllegalArgumentException("Parameter for constraint ["+ConstrainedProperty.CREDIT_CARD_CONSTRAINT+"] of property ["+constraintPropertyName+"] of class ["+constraintOwningClass+"] must be a boolean value");
48 this.creditCard = ((Boolean)constraintParameter).booleanValue();
49 super.setParameter(constraintParameter);
52 public String getName() {
53 return ConstrainedProperty.CREDIT_CARD_CONSTRAINT;
56 public boolean supports(Class type) {
57 return type != null && String.class.isAssignableFrom(type);