Class DefaultFormValidatorResults

  • All Implemented Interfaces:
    FormValidatorResults

    public class DefaultFormValidatorResults
    extends Object
    implements FormValidatorResults

    Default implementation of the FormValidatorResults interface.

    This class provides a fully functional implementation of the FormValidatorResults interface. Instances are initialized with a map that contains the names of the validated fields and their corresponding ValidationResult objects. They are immutable and thus can be shared between multiple threads.

    Version:
    $Id: DefaultFormValidatorResults.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • DefaultFormValidatorResults

        public DefaultFormValidatorResults​(Map<String,​? extends ValidationResult> fieldData)
        Creates a new instance of DefaultFormValidatorResults and initializes it with a map holding information about fields and their validation status.
        Parameters:
        fieldData - the map with the field data (must not be null
        Throws:
        IllegalArgumentException - if the map is null or contains null values
    • Method Detail

      • validResultMapForForm

        public static Map<String,​ValidationResult> validResultMapForForm​(Form form)
        Creates a java.util.Map with validation information for all fields of the specified Form. Each field is assigned a valid ValidationResult object. This method can be used by custom FormValidator implementations to initialize a map with data about all form fields. Then, during validation, fields that were detected to be invalid can be updated in this map. Finally, a DefaultFormValidatorResults object can be created based on this map.
        Parameters:
        form - the Form (must not be null)
        Returns:
        a map with valid validation data for all fields of the form
        Throws:
        IllegalArgumentException - if the Form is null
      • validResultsForForm

        public static DefaultFormValidatorResults validResultsForForm​(Form form)
        Creates a valid DefaultFormValidatorResults for the fields of the specified Form. The resulting object stores a valid ValidationResult object for all fields that belong to the form.
        Parameters:
        form - the Form (must not be null)
        Returns:
        a valid DefaultFormValidatorResults object for this form
        Throws:
        IllegalArgumentException - if the Form is null
      • createValidationMessage

        public static ValidationMessage createValidationMessage​(Form form,
                                                                String key,
                                                                Object... params)
        Obtains a ValidationMessage object for the specified key. The message is obtained from the current ValidationMessageHandler (which can be retrieved from the form's TransformerContext).
        Parameters:
        form - the Form (must not be null)
        key - the key of the validation error message
        params - additional parameters for the validation error message
        Returns:
        a ValidationMessage object for the specified error message
        Throws:
        IllegalArgumentException - if the Form is null
      • createValidationErrorResult

        public static ValidationResult createValidationErrorResult​(Form form,
                                                                   String key,
                                                                   Object... params)
        Creates a ValidationResult object with an error message for a validation message. This is a convenience method that obtains the validation error message from the current ValidationMessageHandler and creates the corresponding ValidationResult object. It can be used for instance by a FormValidator that determines an incorrect field.
        Parameters:
        form - the Form (must not be null)
        key - the key of the validation error message
        params - additional parameters for the validation error message
        Returns:
        a ValidationResult object initialized with this error message
        Throws:
        IllegalArgumentException - if the Form is null
      • merge

        public static FormValidatorResults merge​(FormValidatorResults res1,
                                                 FormValidatorResults res2)
        Returns a FormValidatorResults object with the combined information of the specified FormValidatorResults objects. If one of the passed in objects is null, the other one is returned. Otherwise a new FormValidatorResults instance is created and populated with the ValidationResult objects contained in both parameter objects. If necessary, the ValidationResult objects are also merged, so that the resulting object actually contains a union of all validation messages.
        Parameters:
        res1 - the first FormValidatorResults object
        res2 - the second FormValidatorResults object
        Returns:
        the merged results
      • isValid

        public boolean isValid()
        Checks whether form validation was successful.
        Specified by:
        isValid in interface FormValidatorResults
        Returns:
        a flag if all fields are valid
      • getErrorFieldNames

        public Set<String> getErrorFieldNames()
        Returns a set with the names of those fields that are invalid.
        Specified by:
        getErrorFieldNames in interface FormValidatorResults
        Returns:
        a set with the names of the invalid fields
      • getResultsFor

        public ValidationResult getResultsFor​(String field)
        Returns the validation results for the specified field or null if this field does not exist.
        Specified by:
        getResultsFor in interface FormValidatorResults
        Parameters:
        field - the name of the desired field
        Returns:
        the validation result for this field
      • equals

        public boolean equals​(Object obj)
        Compares this object with another one. Two form validator result objects are considered equal if and only if they contain the same set of fields with corresponding result objects.
        Overrides:
        equals in class Object
        Parameters:
        obj - the object to compare to
        Returns:
        a flag whether the objects are equal
      • hashCode

        public int hashCode()
        Determines a hash code for this object.
        Overrides:
        hashCode in class Object
        Returns:
        a hash code
      • toString

        public String toString()
        Returns a string representation for this object. This string contains the string representation for the internal map with the fields and their validation result objects. Provided that the ValidationResult objects have proper toString() implementations, the string generated by the map is exactly what is needed to see which field has which validation status.
        Overrides:
        toString in class Object
        Returns:
        a string for this object