Search in sources :

Example 1 with ClientConstraintDescriptor

use of org.apache.tapestry5.beanvalidator.ClientConstraintDescriptor in project tapestry-5 by apache.

the class BeanFieldValidator method render.

@Override
public void render(final MarkupWriter writer) {
    final BeanValidationContext beanValidationContext = environment.peek(BeanValidationContext.class);
    if (beanValidationContext == null) {
        return;
    }
    final Validator validator = validatorFactory.getValidator();
    final String currentProperty = beanValidationContext.getCurrentProperty();
    if (currentProperty == null)
        return;
    final ValidationInfo validationInfo = getValidationInfo(beanValidationContext, currentProperty, validator);
    final PropertyDescriptor propertyDescriptor = validationInfo.getPropertyDescriptor();
    if (propertyDescriptor == null)
        return;
    for (final ConstraintDescriptor<?> descriptor : propertyDescriptor.getConstraintDescriptors()) {
        Class<? extends Annotation> annotationType = descriptor.getAnnotation().annotationType();
        ClientConstraintDescriptor clientConstraintDescriptor = clientValidatorSource.getConstraintDescriptor(annotationType);
        if (clientConstraintDescriptor == null) {
            continue;
        }
        String message = format("%s %s", field.getLabel(), interpolateMessage(descriptor));
        Map<String, Object> attributes = CollectionFactory.newMap();
        for (String attribute : clientConstraintDescriptor.getAttributes()) {
            Object object = descriptor.getAttributes().get(attribute);
            if (object == null) {
                throw new NullPointerException(String.format("Attribute '%s' of %s is null but is required to apply client-side validation.", attribute, descriptor));
            }
            attributes.put(attribute, object);
        }
        clientConstraintDescriptor.applyClientValidation(writer, message, attributes);
    }
}
Also used : ClientConstraintDescriptor(org.apache.tapestry5.beanvalidator.ClientConstraintDescriptor) PropertyDescriptor(javax.validation.metadata.PropertyDescriptor) BeanValidationContext(org.apache.tapestry5.internal.BeanValidationContext) Validator(javax.validation.Validator) FieldValidator(org.apache.tapestry5.FieldValidator)

Aggregations

Validator (javax.validation.Validator)1 PropertyDescriptor (javax.validation.metadata.PropertyDescriptor)1 FieldValidator (org.apache.tapestry5.FieldValidator)1 ClientConstraintDescriptor (org.apache.tapestry5.beanvalidator.ClientConstraintDescriptor)1 BeanValidationContext (org.apache.tapestry5.internal.BeanValidationContext)1