1 16 package org.apache.cocoon.forms.validation.impl; 17 18 import org.apache.cocoon.forms.datatype.ValidationRule; 19 import org.apache.cocoon.forms.formmodel.ExpressionContextImpl; 20 import org.apache.cocoon.forms.formmodel.Widget; 21 import org.apache.cocoon.forms.validation.ValidationError; 22 import org.apache.cocoon.forms.validation.ValidationErrorAware; 23 import org.apache.cocoon.forms.validation.WidgetValidator; 24 25 32 public class ValidationRuleValidator implements WidgetValidator { 33 34 private ValidationRule rule; 35 36 public ValidationRuleValidator(ValidationRule rule) { 37 this.rule = rule; 38 } 39 40 public boolean validate(Widget widget) 41 { 42 if (! (widget instanceof ValidationErrorAware)) { 43 throw new IllegalArgumentException ("Widget '" + widget.getRequestParameterName() + "' is not ValidationErrorAware"); 45 } 46 47 Object value = widget.getValue(); 48 if (value == null) { 49 return true; 51 52 } else { 53 ValidationError error = this.rule.validate(value, new ExpressionContextImpl(widget)); 55 if (error != null) { 56 ((ValidationErrorAware)widget).setValidationError(error); 58 return false; 59 } else { 60 return true; 62 } 63 } 64 } 65 } 66 | Popular Tags |