Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 4.0
    • Fix Version/s: 4.1.2
    • Component/s: Framework
    • Labels:
      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>

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

        Activity

        Hide
        Andreas Andreou added a comment -

        Ron, is it possible to attach the files in this issue, instead of copy-pasting them... I believe we have to do it this way so as not to have any licensing issues

        Show
        Andreas Andreou added a comment - Ron, is it possible to attach the files in this issue, instead of copy-pasting them... I believe we have to do it this way so as not to have any licensing issues
        Hide
        Andreas Andreou added a comment -

        Almost finished... just the docs pending

        Show
        Andreas Andreou added a comment - Almost finished... just the docs pending

          People

          • Assignee:
            Andreas Andreou
            Reporter:
            Ron Piterman
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development