Search in sources :

Example 1 with Context

use of javax.validation.MessageInterpolator.Context in project flow by vaadin.

the class BeanValidator method apply.

/**
 * Validates the given value as if it were the value of the bean property
 * configured for this validator. Returns {@code Result.ok} if there are no
 * JSR-303 constraint violations, a {@code Result.error} of chained
 * constraint violation messages otherwise.
 * <p>
 * Null values are accepted unless the property has an {@code @NotNull}
 * annotation or equivalent.
 *
 * @param value
 *            the input value to validate
 * @param context
 *            the value context for validation
 * @return the validation result
 */
@Override
public ValidationResult apply(final Object value, ValueContext context) {
    Set<? extends ConstraintViolation<?>> violations = getJavaxBeanValidator().validateValue(beanType, propertyName, value);
    Locale locale = context.getLocale().orElse(Locale.getDefault());
    Optional<ValidationResult> result = violations.stream().map(violation -> ValidationResult.error(getMessage(violation, locale))).findFirst();
    return result.orElse(ValidationResult.ok());
}
Also used : Locale(java.util.Locale) Validator(com.vaadin.flow.data.binder.Validator) ConstraintDescriptor(javax.validation.metadata.ConstraintDescriptor) Context(javax.validation.MessageInterpolator.Context) Set(java.util.Set) ValidatorFactory(javax.validation.ValidatorFactory) Serializable(java.io.Serializable) Objects(java.util.Objects) ValidationResult(com.vaadin.flow.data.binder.ValidationResult) Locale(java.util.Locale) Optional(java.util.Optional) Validation(javax.validation.Validation) ValueContext(com.vaadin.flow.data.binder.ValueContext) BeanUtil(com.vaadin.flow.internal.BeanUtil) ConstraintViolation(javax.validation.ConstraintViolation) ValidationResult(com.vaadin.flow.data.binder.ValidationResult)

Example 2 with Context

use of javax.validation.MessageInterpolator.Context in project tapestry-5 by apache.

the class BeanFieldValidator method interpolateMessage.

private String interpolateMessage(final ConstraintDescriptor<?> descriptor) {
    String messageTemplate = (String) descriptor.getAttributes().get("message");
    MessageInterpolator messageInterpolator = validatorFactory.getMessageInterpolator();
    return messageInterpolator.interpolate(messageTemplate, new Context() {

        @Override
        public ConstraintDescriptor<?> getConstraintDescriptor() {
            return descriptor;
        }

        @Override
        public Object getValidatedValue() {
            return null;
        }
    });
}
Also used : Context(javax.validation.MessageInterpolator.Context) BeanValidationContext(org.apache.tapestry5.internal.BeanValidationContext) ConstraintDescriptor(javax.validation.metadata.ConstraintDescriptor) ClientConstraintDescriptor(org.apache.tapestry5.beanvalidator.ClientConstraintDescriptor) MessageInterpolator(javax.validation.MessageInterpolator)

Aggregations

Context (javax.validation.MessageInterpolator.Context)2 ConstraintDescriptor (javax.validation.metadata.ConstraintDescriptor)2 ValidationResult (com.vaadin.flow.data.binder.ValidationResult)1 Validator (com.vaadin.flow.data.binder.Validator)1 ValueContext (com.vaadin.flow.data.binder.ValueContext)1 BeanUtil (com.vaadin.flow.internal.BeanUtil)1 Serializable (java.io.Serializable)1 Locale (java.util.Locale)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ConstraintViolation (javax.validation.ConstraintViolation)1 MessageInterpolator (javax.validation.MessageInterpolator)1 Validation (javax.validation.Validation)1 ValidatorFactory (javax.validation.ValidatorFactory)1 ClientConstraintDescriptor (org.apache.tapestry5.beanvalidator.ClientConstraintDescriptor)1 BeanValidationContext (org.apache.tapestry5.internal.BeanValidationContext)1