struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nuwan Chandrasoma <mymail...@gmail.com>
Subject Re: expression validator
Date Fri, 29 Feb 2008 16:08:43 GMT
Hi,

yes. this is what i did for one of the project i am bit involved.

1. wrote a custom validator

public class PasswordConfirmValidator extends FieldValidatorSupport {

     private static Log log = 
LogFactory.getLog(PasswordConfirmValidator.class);

    private String fieldName2;

    public String getFieldName2() {
        return fieldName2;
    }

    public void setFieldName2(String fieldName2) {
        this.fieldName2 = fieldName2;
    }



    public void validate(Object o) throws ValidationException {

        String fieldName = getFieldName();
        String val = (String) getFieldValue(fieldName, o);


        String val2 = (String)getFieldValue(fieldName2,o);


        if (!val.equals(val2)) {
           addFieldError(fieldName, o);
        }

    }
}




2. configured the the customer validator in validators.xml

<validator name="passwordconfirm" 
class="com.xxx.util.validation.PasswordConfirmValidator"/>


3. add a custom javascript in the form-close-validate.ftl

            <#elseif validator.validatorType = "passwordconfirm">
            if (field.value != null) {
                var value =  field.value;
                var field2 = form.elements['${validator.fieldName2}'];
                var value2 = field2.value;

                if (value!=value2) {
                    addError(field, error);
                    errors = true;
                }
            }
4. add a annotation for the field i need to validate.

@CustomValidator(type="passwordconfirm",message = "Confirm password 
should be same as password.",parameters = { @ValidationParameter( name = 
"fieldName2", value = "password" ) })

Thanks,

Nuwan


Randy Burgess wrote:
> One of the things I love about Struts is that you get validation without
> writing any real validation code. Admittedly it would be nice if you got
> client side validation for expressions. As you know you can create a custom
> validator and for the client side stuff you would have to edit
> form-close-validate.ftl in the xhtml template. String matching javascript is
> trivial to write.
>
> Regards,
> Randy Burgess
> Sr. Web Applications Developer
> Nuvox Communications
>
>
>
>   
>> From: mojoRising <jpiser@averoinc.com>
>> Reply-To: Struts Users Mailing List <user@struts.apache.org>
>> Date: Fri, 29 Feb 2008 06:55:51 -0800 (PST)
>> To: <user@struts.apache.org>
>> Subject: Re: expression validator
>>
>>
>> Thanks. But I still don't understand...According to the link you mentioned it
>> is not possible to do what I was trying to. So, what is the point of this
>> validator if it doesn't do client side validation? And, are you saying that
>> it is not possible to do a simple 'equals' test in client side validation?
>>
>>  <validator type="expression">password.equals(password2)
>>     <message>Passwords do not match</message>
>>    </validator> 
>>
>>
>>
>>
>>
>> That validator doesn't do client side validation.
>>
>> Only the following validators are supported:
>>
>>     * required validator
>>     * requiredstring validator
>>     * stringlength validator
>>     * regex validator
>>     * email validator
>>     * url validator
>>     * int validator
>>     * double validator
>>
>> http://struts.apache.org/2.0.11/docs/pure-javascript-client-side-validation.
>> html
>>
>> Regards,
>> Randy Burgess
>> Sr. Web Applications Developer
>> Nuvox Communications
>> -- 
>> View this message in context:
>> http://www.nabble.com/expression-validator-tp15746472p15760197.html
>> Sent from the Struts - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>     
>
>
>
> This email and any attachments ("Message") may contain legally privileged and/or confidential
information.  If you are not the addressee, or if this Message has been addressed to you in
error, you are not authorized to read, copy, or distribute it, and we ask that you please
delete it (including all copies) and notify the sender by return email.  Delivery of this
Message to any person other than the intended recipient(s) shall not be deemed a waiver of
confidentiality and/or a privilege.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message