Example usage for org.apache.wicket.model AbstractPropertyModel getPropertyField

List of usage examples for org.apache.wicket.model AbstractPropertyModel getPropertyField

Introduction

In this page you can find the example usage for org.apache.wicket.model AbstractPropertyModel getPropertyField.

Prototype

@Override
    public Field getPropertyField() 

Source Link

Usage

From source file:com.visural.wicket.behavior.jsr303.JSR303AnnotatedPropertyModelBehavior.java

License:Apache License

private boolean addValidators(Component component) {
    if (FormComponent.class.isAssignableFrom(component.getClass())) {
        FormComponent fc = (FormComponent) component;
        Object model = component.getDefaultModel();
        if (model != null) {
            if (AbstractPropertyModel.class.isAssignableFrom(model.getClass())) {
                AbstractPropertyModel apm = (AbstractPropertyModel) model;
                if (apm.getPropertyField() != null) {
                    Annotation[] annots = apm.getPropertyField().getAnnotations();
                    Long min = null;
                    Long max = null;
                    for (Annotation a : annots) {
                        if (a.annotationType().getName().equals("javax.validation.constraints.NotNull")) {
                            IValidator v = newNotNullValidator();
                            if (v == null) {
                                fc.setRequired(true);
                            } else {
                                fc.add(v);
                            }//from w w w. j  a v  a 2  s .  c om
                        } else if (a.annotationType().getName().equals("javax.validation.constraints.Min")) {
                            min = ((Min) a).value();
                        } else if (a.annotationType().getName().equals("javax.validation.constraints.Max")) {
                            max = ((Max) a).value();
                        } else if (a.annotationType().getName().equals("javax.validation.constraints.Past")) {
                            fc.add(newPastValidator());
                        } else if (a.annotationType().getName().equals("javax.validation.constraints.Future")) {
                            fc.add(newFutureValidator());
                        } else if (a.annotationType().getName()
                                .equals("javax.validation.constraints.Pattern")) {
                            Pattern pattern = (Pattern) a;
                            fc.add(newPatternValidator(pattern.regexp()));
                        } else if (a.annotationType().getName().equals("javax.validation.constraints.Size")) {
                            Size size = (Size) a;
                            fc.add(newSizeValidator(size.min(), size.max()));
                        } else if (a.annotationType().getName().equals(getEmailAnnotationClassName())) {
                            fc.add(newEmailValidator());
                        }
                    }
                    if (max != null || min != null) {
                        fc.add(newMinMaxValidator(min, max));
                    }
                }
            }
            return true;
        }
    }
    return false;
}