Class Validation

java.lang.Object
rife.validation.Validation
All Implemented Interfaces:
Cloneable, Constrained, ConstrainedPropertyListener, Validated

public class Validation extends Object implements Validated, Cloneable, Constrained, ConstrainedPropertyListener
  • Constructor Details

    • Validation

      public Validation()
  • Method Details

    • activateValidation

      protected void activateValidation()
      This method is called at least once and maximum once when any method related to Validated rules, subjects and group or Constrained properties are used.

      By overriding this method, you can thus isolate all the validation setup code and don't enforce a performance penalty at each object construction when doing it in the default constructor.

      Since:
      1.0
    • provideValidatedBean

      public void provideValidatedBean(Validated bean)
      Description copied from interface: Validated
      Provide the bean instance that will be validated.

      By default 'this' will be used.

      Specified by:
      provideValidatedBean in interface Validated
      Parameters:
      bean - the bean instance that will be validated
    • retrieveValidatedBean

      public Validated retrieveValidatedBean()
      Description copied from interface: Validated
      Retrieves the bean instance that will be validated.
      Specified by:
      retrieveValidatedBean in interface Validated
    • addGroup

      public ValidationGroup addGroup(String name)
      Description copied from interface: Validated
      Adds a new validation group.
      Specified by:
      addGroup in interface Validated
      Parameters:
      name - the name of the validation group that needs to be created and added
      Returns:
      the newly created ValidationGroup
    • focusGroup

      public void focusGroup(String name)
      Description copied from interface: Validated
      Focuses on one particular validation group, showing only the ValidationErrors that were generated by its ValidationRules.
      Specified by:
      focusGroup in interface Validated
      Parameters:
      name - the name of the validation group that will be focused
    • resetGroup

      public void resetGroup(String name)
      Description copied from interface: Validated
      Removed all the ValidationErrors of a particular validation group.
      Specified by:
      resetGroup in interface Validated
      Parameters:
      name - the name of the validation group that will be focused
    • addRule

      public void addRule(ValidationRule rule)
      Description copied from interface: Validated

      Adds a new validation rule.

      The collection of rules is what is supposed to perform the validation, though any other additional method could be used. At least those rules that have been registered will be evaluated.

      Specified by:
      addRule in interface Validated
      Parameters:
      rule - the rule that will be added
      See Also:
    • generateConstrainedPropertyRules

      public List<PropertyValidationRule> generateConstrainedPropertyRules(ConstrainedProperty constrainedProperty)
      Description copied from interface: Validated
      Generates the validation rules that are related to a particular ConstrainedProperty.
      Specified by:
      generateConstrainedPropertyRules in interface Validated
      Parameters:
      constrainedProperty - the ConstrainedProperty that will be inspected
      Returns:
      the list of generated ValidationRules
    • addConstrainedPropertyRules

      public List<PropertyValidationRule> addConstrainedPropertyRules(ConstrainedProperty constrainedProperty)
      Description copied from interface: Validated
      Adds the validation rules that are related to a particular ConstrainedProperty.

      If the rules of this property name have already been added before through another ConstrainedProperty instance, its existing ValidationRules will be erased and the previous constraints will be merged into the new ConstrainedProperty before adding its validation rules.

      Specified by:
      addConstrainedPropertyRules in interface Validated
      Parameters:
      constrainedProperty - the ConstrainedProperty that will be inspected
      Returns:
      the list of generated ValidationRules
    • addConstraint

      public void addConstraint(ConstrainedProperty constrainedProperty)
      Description copied from interface: Constrained
      Add a new constrained property.

      When several of the same constrained properties are added, they are merged at constraint-level. This means for instance that a previous inList constraint will be replaced by the one of the new constrained bean, they will not be combined.

      Specified by:
      addConstraint in interface Constrained
      Parameters:
      constrainedProperty - the ConstrainedProperty instance that has to be added
      See Also:
    • addConstraint

      public void addConstraint(ConstrainedBean constrainedBean)
      Description copied from interface: Constrained
      Add a new constrained bean.

      When several constrained beans are added, they are merged at constraint-level. This means for instance that all previous unique constraints will be replaced by those of the new constrained bean, they will not be combined.

      Specified by:
      addConstraint in interface Constrained
      Parameters:
      constrainedBean - the ConstrainedBean instance that has to be added
      See Also:
    • addValidationError

      public void addValidationError(ValidationError newError)
      Description copied from interface: Validated
      Add a new validation error explicitly to the collection of already existing errors.

      Note that this method should respect subjects with a limited error amount and only store the first error for these subjects.

      Specified by:
      addValidationError in interface Validated
      Parameters:
      newError - the ValidationError to add
      See Also:
    • getRules

      public List<ValidationRule> getRules()
      Description copied from interface: Validated
      Retrieves that validation rules that have been registered.
      Specified by:
      getRules in interface Validated
      See Also:
    • getConstrainedProperties

      public Collection<ConstrainedProperty> getConstrainedProperties()
      Description copied from interface: Constrained
      Returns a collection with all the constrained properties that have been registered.
      Specified by:
      getConstrainedProperties in interface Constrained
      Returns:
      A Collection with all the ConstrainedProperty objects that are registered. If no constrained properties are available, an empty collection will be returned, not null.
      See Also:
    • hasPropertyConstraint

      public boolean hasPropertyConstraint(String name)
      Description copied from interface: Constrained
      Indicates whether this constrained bean contains a particular constraint on at least one of its properties.
      Specified by:
      hasPropertyConstraint in interface Constrained
      Returns:
      true if this constraint is present on at least one of the properties; or

      false otherwise

      See Also:
    • getConstrainedProperty

      public ConstrainedProperty getConstrainedProperty(String propertyName)
      Description copied from interface: Constrained
      Retrieve a registered ConstrainedProperty according to its name.
      Specified by:
      getConstrainedProperty in interface Constrained
      Parameters:
      propertyName - the name of the ConstrainedProperty that has to be retrieved
      Returns:
      the requested ConstrainedProperty; or

      null if no such ConstrainedProperty is available.

      See Also:
    • getGroups

      public Collection<ValidationGroup> getGroups()
      Description copied from interface: Validated
      Retrieves all validation groups.
      Specified by:
      getGroups in interface Validated
      Returns:
      the collection of all registered validation groups
    • getGroup

      public ValidationGroup getGroup(String name)
      Description copied from interface: Validated
      Retrieve a particular validation group.
      Specified by:
      getGroup in interface Validated
      Parameters:
      name - the name of the validation group that will be retrieved
      Returns:
      the requested ValidationGroup; or

      null if no such validation group exists

    • getConstrainedBean

      public ConstrainedBean getConstrainedBean()
      Description copied from interface: Constrained
      Retrieves the constrained bean that has been set for this Constrained instance.
      Specified by:
      getConstrainedBean in interface Constrained
      Returns:
      the requested ConstrainedBean; or

      null if no ConstrainedBean is available.

      See Also:
    • validate

      public boolean validate()
      Description copied from interface: Validated
      Validates the internal subjects.

      This method is not supposed to reset the validation errors or to start the validation from scratch, but it's intended to add additional errors to an existing collection.

      Specified by:
      validate in interface Validated
      Returns:
      true if no validation errors are present after the validation; or

      false if validation errors are available.

      See Also:
    • validate

      public boolean validate(ValidationContext context)
      Description copied from interface: Validated
      Validates the internal subjects and also validates the bean within the provided ValidationContext

      This method is not supposed to reset the validation errors or to start the validation from scratch, but it's intended to add additional errors to an existing collection.

      Specified by:
      validate in interface Validated
      Parameters:
      context - the ValidationContext in which this bean instance will be additionally validated
      Returns:
      true if no validation errors are present after the validation; or

      false if validation errors are available.

      See Also:
    • validateGroup

      public boolean validateGroup(String name)
      Description copied from interface: Validated
      Validate the ValidationRules of a particular validation group.
      Specified by:
      validateGroup in interface Validated
      Parameters:
      name - the name of the validation group that will be retrieved
      Returns:
      true if no validation errors were generated; or

      false otherwise

    • validateGroup

      public boolean validateGroup(String name, ValidationContext context)
      Description copied from interface: Validated
      Validate the ValidationRules of a particular validation group and also validates the entire bean within the provided ValidationContext
      Specified by:
      validateGroup in interface Validated
      Parameters:
      name - the name of the validation group
      context - the ValidationContext in which this bean instance will be additionally validated
      Returns:
      true if no validation errors were generated; or

      false otherwise

    • countValidationErrors

      public int countValidationErrors()
      Description copied from interface: Validated
      Counts the number of stored ValidationErrors.
      Specified by:
      countValidationErrors in interface Validated
      Returns:
      The number of stored ValidationErrors.
    • resetValidation

      public void resetValidation()
      Description copied from interface: Validated

      Resets the validation by removing all validation errors that are currently present.

      This method is typically used to start a new validation from scratch or to re-validate until all errors have been solved.

      Specified by:
      resetValidation in interface Validated
      See Also:
    • getValidationErrors

      public Set<ValidationError> getValidationErrors()
      Description copied from interface: Validated
      Returns a set with all the stored ValidationErrors.
      Specified by:
      getValidationErrors in interface Validated
      Returns:
      A Set instance with all the stored ValidationErrors. Note that when no errors are available an empty set is returned, not null.
    • replaceValidationErrors

      public void replaceValidationErrors(Set<ValidationError> errors)
      Description copied from interface: Validated
      Replaces the stored ValidationErrors with a new set of errors.
      Specified by:
      replaceValidationErrors in interface Validated
      Parameters:
      errors - the Set instance that contains all the ValidationErrors that have to be stored.
    • limitSubjectErrors

      public void limitSubjectErrors(String subject)
      Description copied from interface: Validated
      Limits the number of errors for a particular subject so that maximum one ValidationError can be stored for it.
      Specified by:
      limitSubjectErrors in interface Validated
      Parameters:
      subject - the name of the subject that has to be limited.
    • unlimitSubjectErrors

      public void unlimitSubjectErrors(String subject)
      Description copied from interface: Validated
      Unlimits the number of errors for a particular subject so that any number of ValidationErrors can be stored for it.
      Specified by:
      unlimitSubjectErrors in interface Validated
      Parameters:
      subject - the name of the subject that has to be unlimited.
    • isSubjectValid

      public boolean isSubjectValid(String subject)
      Description copied from interface: Validated
      Checks if a subject is valid.

      This is determined by verifying if there are ValidationErrors present for it. This method will thus not execute a validation action.

      Specified by:
      isSubjectValid in interface Validated
      Parameters:
      subject - the name of the subject that has to be checked.
      Returns:
      true when no errors could be found for the subject; or

      false when errors are present for the subject.

      See Also:
    • makeSubjectValid

      public void makeSubjectValid(String subject)
      Description copied from interface: Validated
      Makes a subject valid.

      This is done by removing all ValidationErrors that are stored for it.

      Specified by:
      makeSubjectValid in interface Validated
      Parameters:
      subject - the name of the subject that has to be made valid.
    • makeErrorValid

      public void makeErrorValid(String identifier, String subject)
      Description copied from interface: Validated
      Makes errors for a particular subject and identifier valid.

      This is done by removing all ValidationErrors that are stored with this identifier and subject.

      Specified by:
      makeErrorValid in interface Validated
      Parameters:
      identifier - the name of the error identifier that has to be made
      subject - the name of the subject that has to be made valid. valid.
    • getValidatedSubjects

      public List<String> getValidatedSubjects()
      Description copied from interface: Validated
      Returns the list of subjects that this object is able to validate internally through the validate() method.
      Specified by:
      getValidatedSubjects in interface Validated
      Returns:
      a List instance with the names of the internally validated subjects
    • getErrorIndication

      public static String getErrorIndication(Validated validated, String subject, String valid, String error)
    • getLoadingErrors

      public Collection<String> getLoadingErrors(String propertyName)
      Description copied from interface: Validated
      Returns the collection of error messages that occurred during the loading of the content of a certain property.
      Specified by:
      getLoadingErrors in interface Validated
      Parameters:
      propertyName - the name of the property whose loading errors should be obtained
      Returns:
      null if no errors occurred during the loading of the content of the provided property or if the property doesn't exist; or

      the requested collection of error messages

    • constraintSet

      public void constraintSet(ConstrainedProperty property, String name, Object constraintData)
      Description copied from interface: ConstrainedPropertyListener
      This method is called on a registered listener when a constraint has been set to a particular value.
      Specified by:
      constraintSet in interface ConstrainedPropertyListener
      Parameters:
      property - the ConstrainedProperty instance where the constraint has been set
      name - the name of the constraint
      constraintData - the data that the constraint has been set to
    • clone

      public Object clone() throws CloneNotSupportedException
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException