You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by James Hays <ja...@mac.com> on 2003/08/14 21:40:45 UTC

Validating Multiple Fields

I wishing to write a change password validator to validate 3 fields.  
The criteria will be that the currently logged in user, in the session, 
will have a password that matches the string typed into an 'oldPassword' 
component.  Next, I will need to compare if the 'newPassword' and 
'newPasswordConfirm' first of all match, and secondly that they are of a 
certian length.

Can I do this all in one validator?

James


Re: Validating Multiple Fields

Posted by Luis Neves <ln...@netcabo.pt>.
Hi,

On Thursday 14 August 2003 20:40, James Hays wrote:
> I wishing to write a change password validator to validate 3 fields.
> The criteria will be that the currently logged in user, in the session,
> will have a password that matches the string typed into an 'oldPassword'
> component.  Next, I will need to compare if the 'newPassword' and
> 'newPasswordConfirm' first of all match, and secondly that they are of a
> certian length.
>
> Can I do this all in one validator?

I don't think so.
To use Group validation (a term coined by MindBridge) you have to use custom 
code in your form listener, Group validation can only occur when all form 
values are available... that's the bad news.
The good news is that you can still leverage the Validation framework.

The following is pseudo code that after tweaking for you situation might help 
you... I hope :-)

Regards,
Luis Neves


--------- 8< Cut here --------- 8<  Cut here --------- 8< Cut here ---------


public void formSubmit(IRequestCycle cycle)
{
  IValidationDelegate delegate =
    (IValidationDelegate) getBeans().getBean("delegate");

  if (delegate.getHasErrors())
    return;

  String oldPassword = getOldPassword();
  String visitPassword = Visit.getPassword();
  String newPassword = getNewPassword();
  String newPasswordConfirm = getNewPasswordConfirm();

  if (!oldPassword.equals(visitPassword))
  {
    IFormComponent fieldOldPassword =
      (IFormComponent) getComponent("fieldOldPassword");
    delegate.setFormComponent(fieldOldPassword);
    delegate.record("The old password you entered doesn't match our records.",
      ValidationConstraint.CONSISTENCY);
    return;
  }

  if (!newPassword.equals(newPasswordConfirm))
  {
    IFormComponent fieldPasswordConfirm =
      (IFormComponent) getComponent("fieldPasswordConfirm");
    delegate.setFormComponent(fieldPasswordConfirm);
    delegate.record("Your entered passwords don't match.",
      ValidationConstraint.CONSISTENCY);
    return;
  }

 ...

  check the length
 ...

 ...

  If you are here the Validation passed
 ...
}

--------- 8< Cut here --------- 8<  Cut here --------- 8< Cut here ---------



Re: Validating Multiple Fields

Posted by Ernest Hill <eh...@loopone.com>.
Here's an example of how I solved a similar problem. I'm not a Tapestry
expert, so I would be very interested in hearing about other solutions:

excerpt from .page file

<bean name="passwordValidator"
class="com.loopone.osiris.valid.PasswordValidator" >
  </bean>

 <component id="newPassword" type="TextField" >
    <binding name="value" expression="visit.userProfile.newPassword"/>
    <binding name="hidden" expression="true" />
    <binding name="displayWidth" expression="20" />
    <binding name="maximumLength" expression="20" />
    <static-binding name="tabindex">86</static-binding>
  </component>

  <component id="confirmPassword" type="ValidField" >
    <binding name="value"
expression="visit.userProfile.confirmedPassword"/>
    <binding name="validator" expression="beans.passwordValidator"/>
    <static-binding name="displayName">Password:</static-binding>
    <binding name="password" expression="components.newPassword"/>
    <binding name="hidden" expression="true" />
    <binding name="displayWidth" expression="20" />
    <binding name="maximumLength" expression="20" />
    <static-binding name="tabindex">87</static-binding>
  </component>

Validator code


/*
 * PasswordValidator.java
 *
 * Created on May 28, 2003, 3:37 PM
 */

package com.loopone.osiris.valid;
import net.sf.tapestry.*;
import net.sf.tapestry.valid.*;
import net.sf.tapestry.form.TextField;


/**
 *
 * @author  ehill
 */
public class PasswordValidator extends net.sf.tapestry.valid.BaseValidator {

    /** Creates a new instance of PasswordValidator */
    public PasswordValidator() {
    }

    /**  Converts input, submitted by the client, into an object value.
     *  May return null if the input is null (and the required flag is
false).
     *
     *  <p>The input string will already have been trimmed.  It may be null.
     *
     *  @throws ValidatorException if the string cannot be converted into
     *  an object, or the object is
     *  not valid (due to other constraints).
     *
     */
    public Object toObject(IField field, String input) throws
ValidatorException {

        IBinding binding = field.getBinding("password");
        TextField passwordField = (TextField) binding.getObject();
        String password = passwordField.readValue();
        if ( password != null ) {
            password = password.trim();
        }

        if ( input == null ) {
            if ( password == null ) {
                return null;
            }
        } else {
            input = input.trim();
        }

        if ( input.equals("") &&  password.equals("")) {
            passwordField.updateValue(null);
            return null;
        }


        if ( ! input.equals(password)) {
            throw new ValidatorException("Value must match password",
OsirisValidationConstraint.PASSWORD_CONSTRAINT, input);
        }

        if ( input.length() < 6 ) {
            throw new ValidatorException("Password must be at least 6
characters", OsirisValidationConstraint.PASSWORD_CONSTRAINT, input);
        }

        return input;


    }

    /**  Invoked during renderring to convert an object value (which may be
null)
     *  to a String.  It is acceptible to return null.  The string will be
the
     *  VALUE attribute of the HTML text field.
     *
     *
     */
    public String toString(IField field, Object value) {

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

        return value.toString();
    }

}

> I wishing to write a change password validator to validate 3 fields.
> The criteria will be that the currently logged in user, in the session,
> will have a password that matches the string typed into an 'oldPassword'
>  component.  Next, I will need to compare if the 'newPassword' and
> 'newPasswordConfirm' first of all match, and secondly that they are of a
>  certian length.
>
> Can I do this all in one validator?
>
> James
>
>
> --------------------------------------------------------------------- To
> unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org For
> additional commands, e-mail: tapestry-user-help@jakarta.apache.org