Uploaded image for project: 'Tapestry'
  1. Tapestry
  2. TAPESTRY-410

IdentityValidator

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 4.0
    • 4.1.2
    • Framework
    • None

    Description

      The following class compares the validated field against another field. It works in two modes:
      match=<fieldname> and differ=<fieldname>

      Cheers,
      Ron

      package org.apache.tapestry.form.valid;

      import org.apache.tapestry.form.IFormComponent;
      import org.apache.tapestry.form.TextField;
      import org.apache.tapestry.form.ValidationMessages;
      import org.apache.tapestry.form.validator.BaseValidator;

      public class IdentityValidator extends BaseValidator {
      private String _fieldName;
      private int _matchType;
      private String _identityMessage;

      private static final int DIFFER = 0;
      private static final int MATCH = 1;

      public IdentityValidator()

      { super(); }

      public IdentityValidator(String initializer)

      { super(initializer); }

      public String toString(IFormComponent field, Object value)

      { if (value == null) return null; return value.toString(); }

      public void validate(IFormComponent field, ValidationMessages messages, Object object)
      throws ValidatorException

      { TextField referent = (TextField)field.getContainer().getComponent(_fieldName); //TODO: if component is null treat _fieldName as an ognl expression boolean notEq = notEqual ( referent.getValue() , object ); if ( _matchType == MATCH ? notEq : !notEq ) throw new ValidatorException(buildIdentityMessage(messages, field, referent), ValidationConstraint.CONSISTENCY); }

      public String getMatch()

      { return _fieldName; }

      public void setMatch(String field)

      { _fieldName = field; _matchType = MATCH; }

      public String getDiffer()

      { return _fieldName; }

      public void setDiffer(String field)

      { _fieldName = field; _matchType = DIFFER; }

      /** @since 3.0 */
      public String getIdentityMessage()

      { return _identityMessage; }

      /**

      • Overrides the <code>field-too-short</code> bundle key. Parameter {0}

        is the minimum length.

      • Parameter {1}

        is the display name of the field.

      • @since 3.0
        */

      public void setMinimumLengthMessage(String string)

      { _identityMessage = string; }

      /** @since 3.0 */

      protected String buildIdentityMessage(ValidationMessages messages, IFormComponent field, IFormComponent referent)
      {
      Object[] parameters = new Object[]

      { field.getDisplayName(), _matchType, referent.getDisplayName() }

      ;
      return messages.formatValidationMessage(_identityMessage,
      "invalid-field-equality", parameters);

      }

      private boolean notEqual(Object o1, Object o2)

      { if (o1 == null && o2 == null) return false; if (o1 == null || o2 == null) return true; return !o1.equals(o2); }

      }

      ==============================

      hivemind:

      <contribution configuration-id="tapestry.form.validator.Validators">
      <validator name="match" configurable="true"
      class="org.apache.tapestry.form.valid.IdentityValidator"/>
      </contribution>

      <contribution configuration-id="tapestry.form.validator.Validators">
      <validator name="differ" configurable="true"
      class="org.apache.tapestry.form.valid.IdentityValidator"/>
      </contribution>

      =========================

      Attachments

        Activity

          People

            andyhot Andreas Andreou
            ronp Ron Piterman
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: