Class BeanValidator

java.lang.Object
com.vaadin.flow.data.validator.BeanValidator
All Implemented Interfaces:
Validator<Object>, Serializable, BiFunction<Object,ValueContext,ValidationResult>

public class BeanValidator extends Object implements Validator<Object>
A Validator using the JSR-303 (jakarta.validation) annotation-based bean validation mechanism. Values passed to this validator are compared against the constraints, if any, specified by annotations on the corresponding bean property.

Note that a JSR-303 implementation (for instance Hibernate Validator or Apache BVal) must be present on the project classpath when using bean validation. Specification versions 1.0 and 1.1 are supported.

Since:
1.0.
Author:
Vaadin Ltd
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    BeanValidator(Class<?> beanType, String propertyName)
    Creates a new JSR-303 BeanValidator that validates values of the specified property against the constraints of the default validation group.
    BeanValidator(Class<?> beanType, String propertyName, com.vaadin.flow.function.SerializableSupplier<Class<?>[]> validationGroupsSupplier)
    Creates a new JSR-303 BeanValidator that validates values of the specified property against the constraints of the validation groups provided by the given supplier.
    BeanValidator(Class<?> beanType, String propertyName, Class<?>... validationGroups)
    Creates a new JSR-303 BeanValidator that validates values of the specified property against the constraints of the given validation groups.
  • Method Summary

    Modifier and Type
    Method
    Description
    apply(Object value, ValueContext context)
    Validates the given value as if it were the value of the bean property configured for this validator.
    protected jakarta.validation.MessageInterpolator.Context
    createContext(jakarta.validation.ConstraintViolation<?> violation)
    Creates a simple message interpolation context based on the given constraint violation.
    jakarta.validation.Validator
    Returns a shared JSR-303 validator instance to use.
    protected static jakarta.validation.ValidatorFactory
    Returns the underlying JSR-303 bean validator factory used.
    protected String
    getMessage(jakarta.validation.ConstraintViolation<?> violation, Locale locale)
    Returns the interpolated error message for the given constraint violation using the locale specified for this validator.
    Class<?>[]
    Gets the validation groups this validator validates against.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.util.function.BiFunction

    andThen
  • Constructor Details

    • BeanValidator

      public BeanValidator(Class<?> beanType, String propertyName)
      Creates a new JSR-303 BeanValidator that validates values of the specified property against the constraints of the default validation group. Localizes validation messages using the default locale.
      Parameters:
      beanType - the bean type declaring the property, not null
      propertyName - the property to validate, not null
      Throws:
      IllegalStateException - if BeanUtil.checkBeanValidationAvailable() returns false
    • BeanValidator

      public BeanValidator(Class<?> beanType, String propertyName, Class<?>... validationGroups)
      Creates a new JSR-303 BeanValidator that validates values of the specified property against the constraints of the given validation groups. Localizes validation messages using the default locale.

      Note that the validation groups replace, rather than extend, the default group: pass Default.class explicitly to validate the constraints that do not declare a group as well.

      Parameters:
      beanType - the bean type declaring the property, not null
      propertyName - the property to validate, not null
      validationGroups - the validation groups to validate against, or none to use the default group
      Throws:
      IllegalStateException - if BeanUtil.checkBeanValidationAvailable() returns false
      IllegalArgumentException - if any of the given validation groups is not an interface
      Since:
      25.3
    • BeanValidator

      public BeanValidator(Class<?> beanType, String propertyName, com.vaadin.flow.function.SerializableSupplier<Class<?>[]> validationGroupsSupplier)
      Creates a new JSR-303 BeanValidator that validates values of the specified property against the constraints of the validation groups provided by the given supplier. The supplier is queried on every validation, which allows the validation groups to be changed after the validator has been created.
      Parameters:
      beanType - the bean type declaring the property, not null
      propertyName - the property to validate, not null
      validationGroupsSupplier - supplier of the validation groups to validate against, not null
      Throws:
      IllegalStateException - if BeanUtil.checkBeanValidationAvailable() returns false
      Since:
      25.3
  • Method Details

    • getValidationGroups

      public Class<?>[] getValidationGroups()
      Gets the validation groups this validator validates against. An empty array means that the default group is used.
      Returns:
      the validation groups, not null
      Since:
      25.3
    • apply

      public ValidationResult apply(Object value, ValueContext context)
      Validates the given value as if it were the value of the bean property configured for this validator. Returns Result.ok if there are no JSR-303 constraint violations, a Result.error of chained constraint violation messages otherwise.

      Null values are accepted unless the property has an @NotNull annotation or equivalent.

      Only the constraints belonging to the validation groups of this validator are taken into account.

      Specified by:
      apply in interface BiFunction<Object,ValueContext,ValidationResult>
      Specified by:
      apply in interface Validator<Object>
      Parameters:
      value - the input value to validate
      context - the value context for validation
      Returns:
      the validation result
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getJavaxBeanValidatorFactory

      protected static jakarta.validation.ValidatorFactory getJavaxBeanValidatorFactory()
      Returns the underlying JSR-303 bean validator factory used. A factory is created using Validation if necessary.
      Returns:
      the validator factory to use
    • getJavaxBeanValidator

      public jakarta.validation.Validator getJavaxBeanValidator()
      Returns a shared JSR-303 validator instance to use.
      Returns:
      the validator to use
    • getMessage

      protected String getMessage(jakarta.validation.ConstraintViolation<?> violation, Locale locale)
      Returns the interpolated error message for the given constraint violation using the locale specified for this validator.
      Parameters:
      violation - the constraint violation
      locale - the used locale
      Returns:
      the localized error message
    • createContext

      protected jakarta.validation.MessageInterpolator.Context createContext(jakarta.validation.ConstraintViolation<?> violation)
      Creates a simple message interpolation context based on the given constraint violation.
      Parameters:
      violation - the constraint violation
      Returns:
      the message interpolation context