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