You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by James Carr <ja...@gmail.com> on 2009/08/12 23:30:38 UTC

Validating that two fields match?

Hello,

Is there something out of the box that will let me validate if two
fields match in struts2? An example would be a form with email and
confirmingEmail fields.

Thanks,
James

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


Re: Validating that two fields match?

Posted by Burton Rhodes <bu...@gmail.com>.
If you can validate server-side, adding this functionality to your
validate function is about as basic as it gets. Do you require client
side validation?

On 8/12/09, James Carr <ja...@gmail.com> wrote:
> Hello,
>
> Is there something out of the box that will let me validate if two
> fields match in struts2? An example would be a form with email and
> confirmingEmail fields.
>
> Thanks,
> James
>
> ---------------------------------------------------------------------
> 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


Re: Validating that two fields match?

Posted by James Carr <ja...@gmail.com>.
I got it working with fieldexpression, but had to use .equals() vs. ==

Now my next question... in order to get the confirmation field to be
highlighted in red, I needed to add the same field validator for this
field as well. I need the other validation errors to display for the
confirm field (like "is required" and "must be valid") but I want to
suppress the message for "they should match" as it should just display
once. How can I do this without resorting to <s:if> ?

Thanks,
james

On Thu, Aug 13, 2009 at 9:17 AM, Martin Gainty<mg...@hotmail.com> wrote:
>
> http://struts.apache.org/2.1.2/docs/using-field-validators.html
>
> <s:form ...>
> ........
> <s:textfield label="Required Validator Field" name="requiredValidatorField" />
> <s:textfield label="Field Expression Validator Field" name="fieldExpressionValidatorField" />
> .....
> </s:form>
> package org.apache.struts2.showcase.validation;
> public class FieldValidatorsExampleAction extends AbstractValidationActionSupport
> {
>    private String requiredValidatorField = null;
>    public String getRequiredValidatorField() {
>        return requiredValidatorField;
>    }
>    public void setRequiredValidatorField(String requiredValidatorField) {
>        this.requiredValidatorField = requiredValidatorField;
>    }
>
>    private String fieldExpressionValidatorField = null;
>    public String getFieldExpressionValidatorField()
>    {
>        return fieldExpressionValidatorField;
>    }
>    public void setFieldExpressionValidatorField(String fieldExpressionValidatorField)
>    {
>        this.fieldExpressionValidatorField = fieldExpressionValidatorField;
>    }
>    }
> }
>
> WEB-INF/classes/validator.xml
> <validators>
> ........
>        <field name="requiredValidatorField">
>                <field-validator type="required">
>                        <message><![CDATA[ required ]]></message>
>                </field-validator>
>        </field>
>        <field name="fieldExpressionValidatorField">
>                <field-validator type="fieldexpression">
>                        <param name="expression">(fieldExpressionValidatorField == requiredValidatorField)</param>
>                        <message><![CDATA[ must be the same as the Required Validator Field if specified ]]></message>
>                </field-validator>
>        </field>
> ..........
> </validators>Martin Gainty
> ______________________________________________
> Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
>
> Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
> Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.
>
>
>
>
>> To: user@struts.apache.org
>> Subject: Re: Validating that two fields match?
>> Date: Thu, 13 Aug 2009 10:06:38 -0400
>> From: musomesa@aol.com
>>
>>
>>  try the pattern
>>
>> <field name="model.pointOfContact.emailAddress">
>> etc
>>
>>
>>
>>
>>
>>
>>
>> -----Original Message-----
>> From: James Carr <ja...@gmail.com>
>> To: Struts Users Mailing List <us...@struts.apache.org>
>> Sent: Thu, Aug 13, 2009 9:53 am
>> Subject: Re: Validating that two fields match?
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> I have tried this and it doesn't seem to work at all. I am using
>> ModelDriven and the model object has a PointOfContact object with an
>> emailAddress and confirmingEmail attributes.
>>
>> <field name="pointOfContact.emailAddress">
>>         <field-validator type="requiredstring">
>>             <param name="trim">true</param>
>>             <message>Email Address is required.</message>
>>         </field-validator>
>>         <field-validator type="email">
>>            <message>Please enter a valid email</message>
>>         </field-validator>
>>         <field-validator type="expression">
>>            <param name="expression">(pointOfContact.emailAddress.equals(pointOfContact.confirmingEmail))</param>
>>            <message>Verify that you have entered the same email
>> address in both fields</message>
>>         </field-validator>
>>     </field>
>>
>> The other two validators above it work.
>>
>> Thanks,
>> James
>>
>> On Wed, Aug 12, 2009 at 5:59 PM, <mu...@aol.com> wrote:
>> >
>> >  Check out
>> > http://struts.apache.org/2.x/docs/expression-validator.html
>> >
>> > If you are using annotations check out
>> >
>> >  http://struts.apache.org/2.x/docs/expressionvalidator-annotation.html
>> >
>> > Your expression will be something like
>> > @ExpressionValidator(expression="email.equals(confirmEmail)",? message="The
>> email fields do not match")
>> >
>> >
>> >
>> >  Chris
>> >
>> >
>> >
>> >
>> > -----Original Message-----
>> > From: James Carr <ja...@gmail.com>
>> > To: Struts Users Mailing List <us...@struts.apache.org>
>> > Sent: Wed, Aug 12, 2009 5:30 pm
>> > Subject: Validating that two fields match?
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> > Hello,
>> >
>> > Is there something out of the box that will let me validate if two
>> > fields match in struts2? An example would be a form with email and
>> > confirmingEmail fields.
>> >
>> > Thanks,
>> > James
>> >
>> > ---------------------------------------------------------------------
>> > 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
>>
>>
>>
>>
>>
>>
>
> _________________________________________________________________
> Get your vacation photos on your phone!
> http://windowsliveformobile.com/en-us/photos/default.aspx?&OCID=0809TL-HM

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


RE: Validating that two fields match?

Posted by Martin Gainty <mg...@hotmail.com>.
http://struts.apache.org/2.1.2/docs/using-field-validators.html

<s:form ...>
........
<s:textfield label="Required Validator Field" name="requiredValidatorField" />
<s:textfield label="Field Expression Validator Field" name="fieldExpressionValidatorField" />
.....
</s:form>
package org.apache.struts2.showcase.validation;
public class FieldValidatorsExampleAction extends AbstractValidationActionSupport 
{
    private String requiredValidatorField = null;
    public String getRequiredValidatorField() {
        return requiredValidatorField;
    }
    public void setRequiredValidatorField(String requiredValidatorField) {
        this.requiredValidatorField = requiredValidatorField;
    }

    private String fieldExpressionValidatorField = null;
    public String getFieldExpressionValidatorField() 
    {
        return fieldExpressionValidatorField;
    }
    public void setFieldExpressionValidatorField(String fieldExpressionValidatorField) 
    {
        this.fieldExpressionValidatorField = fieldExpressionValidatorField;
    }
    }
}

WEB-INF/classes/validator.xml
<validators>
........
	<field name="requiredValidatorField">
		<field-validator type="required">
			<message><![CDATA[ required ]]></message>
		</field-validator>
	</field>
	<field name="fieldExpressionValidatorField">
		<field-validator type="fieldexpression">
			<param name="expression">(fieldExpressionValidatorField == requiredValidatorField)</param>
			<message><![CDATA[ must be the same as the Required Validator Field if specified ]]></message>
		</field-validator>
	</field>
..........
</validators>Martin Gainty 
______________________________________________ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.




> To: user@struts.apache.org
> Subject: Re: Validating that two fields match?
> Date: Thu, 13 Aug 2009 10:06:38 -0400
> From: musomesa@aol.com
> 
> 
>  try the pattern
> 
> <field name="model.pointOfContact.emailAddress">
> etc
> 
> 
>  
> 
> 
>  
> 
> -----Original Message-----
> From: James Carr <ja...@gmail.com>
> To: Struts Users Mailing List <us...@struts.apache.org>
> Sent: Thu, Aug 13, 2009 9:53 am
> Subject: Re: Validating that two fields match?
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> I have tried this and it doesn't seem to work at all. I am using
> ModelDriven and the model object has a PointOfContact object with an
> emailAddress and confirmingEmail attributes.
> 
> <field name="pointOfContact.emailAddress">
>         <field-validator type="requiredstring">
>             <param name="trim">true</param>
>             <message>Email Address is required.</message>
>         </field-validator>
>         <field-validator type="email">
>            <message>Please enter a valid email</message>
>         </field-validator>
>         <field-validator type="expression">
>            <param name="expression">(pointOfContact.emailAddress.equals(pointOfContact.confirmingEmail))</param>
>            <message>Verify that you have entered the same email
> address in both fields</message>
>         </field-validator>
>     </field>
> 
> The other two validators above it work.
> 
> Thanks,
> James
> 
> On Wed, Aug 12, 2009 at 5:59 PM, <mu...@aol.com> wrote:
> >
> >  Check out
> > http://struts.apache.org/2.x/docs/expression-validator.html
> >
> > If you are using annotations check out
> >
> >  http://struts.apache.org/2.x/docs/expressionvalidator-annotation.html
> >
> > Your expression will be something like
> > @ExpressionValidator(expression="email.equals(confirmEmail)",? message="The 
> email fields do not match")
> >
> >
> >
> >  Chris
> >
> >
> >
> >
> > -----Original Message-----
> > From: James Carr <ja...@gmail.com>
> > To: Struts Users Mailing List <us...@struts.apache.org>
> > Sent: Wed, Aug 12, 2009 5:30 pm
> > Subject: Validating that two fields match?
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > Hello,
> >
> > Is there something out of the box that will let me validate if two
> > fields match in struts2? An example would be a form with email and
> > confirmingEmail fields.
> >
> > Thanks,
> > James
> >
> > ---------------------------------------------------------------------
> > 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
> 
> 
> 
> 
>  
> 

_________________________________________________________________
Get your vacation photos on your phone!
http://windowsliveformobile.com/en-us/photos/default.aspx?&OCID=0809TL-HM

Re: Validating that two fields match?

Posted by mu...@aol.com.
 try the pattern

<field name="model.pointOfContact.emailAddress">
etc


 


 

-----Original Message-----
From: James Carr <ja...@gmail.com>
To: Struts Users Mailing List <us...@struts.apache.org>
Sent: Thu, Aug 13, 2009 9:53 am
Subject: Re: Validating that two fields match?










I have tried this and it doesn't seem to work at all. I am using
ModelDriven and the model object has a PointOfContact object with an
emailAddress and confirmingEmail attributes.

<field name="pointOfContact.emailAddress">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Email Address is required.</message>
        </field-validator>
        <field-validator type="email">
           <message>Please enter a valid email</message>
        </field-validator>
        <field-validator type="expression">
           <param name="expression">(pointOfContact.emailAddress.equals(pointOfContact.confirmingEmail))</param>
           <message>Verify that you have entered the same email
address in both fields</message>
        </field-validator>
    </field>

The other two validators above it work.

Thanks,
James

On Wed, Aug 12, 2009 at 5:59 PM, <mu...@aol.com> wrote:
>
>  Check out
> http://struts.apache.org/2.x/docs/expression-validator.html
>
> If you are using annotations check out
>
>  http://struts.apache.org/2.x/docs/expressionvalidator-annotation.html
>
> Your expression will be something like
> @ExpressionValidator(expression="email.equals(confirmEmail)",? message="The 
email fields do not match")
>
>
>
>  Chris
>
>
>
>
> -----Original Message-----
> From: James Carr <ja...@gmail.com>
> To: Struts Users Mailing List <us...@struts.apache.org>
> Sent: Wed, Aug 12, 2009 5:30 pm
> Subject: Validating that two fields match?
>
>
>
>
>
>
>
>
>
>
> Hello,
>
> Is there something out of the box that will let me validate if two
> fields match in struts2? An example would be a form with email and
> confirmingEmail fields.
>
> Thanks,
> James
>
> ---------------------------------------------------------------------
> 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




 


Re: Validating that two fields match?

Posted by James Carr <ja...@gmail.com>.
I have tried this and it doesn't seem to work at all. I am using
ModelDriven and the model object has a PointOfContact object with an
emailAddress and confirmingEmail attributes.

<field name="pointOfContact.emailAddress">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Email Address is required.</message>
        </field-validator>
	    <field-validator type="email">
	       <message>Please enter a valid email</message>
	    </field-validator>
	    <field-validator type="expression">
	       <param name="expression">(pointOfContact.emailAddress.equals(pointOfContact.confirmingEmail))</param>
           <message>Verify that you have entered the same email
address in both fields</message>
        </field-validator>
    </field>

The other two validators above it work.

Thanks,
James

On Wed, Aug 12, 2009 at 5:59 PM, <mu...@aol.com> wrote:
>
>  Check out
> http://struts.apache.org/2.x/docs/expression-validator.html
>
> If you are using annotations check out
>
>  http://struts.apache.org/2.x/docs/expressionvalidator-annotation.html
>
> Your expression will be something like
> @ExpressionValidator(expression="email.equals(confirmEmail)",? message="The email fields do not match")
>
>
>
>  Chris
>
>
>
>
> -----Original Message-----
> From: James Carr <ja...@gmail.com>
> To: Struts Users Mailing List <us...@struts.apache.org>
> Sent: Wed, Aug 12, 2009 5:30 pm
> Subject: Validating that two fields match?
>
>
>
>
>
>
>
>
>
>
> Hello,
>
> Is there something out of the box that will let me validate if two
> fields match in struts2? An example would be a form with email and
> confirmingEmail fields.
>
> Thanks,
> James
>
> ---------------------------------------------------------------------
> 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


Re: Validating that two fields match?

Posted by mu...@aol.com.
 Check out 
http://struts.apache.org/2.x/docs/expression-validator.html

If you are using annotations check out

 http://struts.apache.org/2.x/docs/expressionvalidator-annotation.html

Your expression will be something like
@ExpressionValidator(expression="email.equals(confirmEmail)",? message="The email fields do not match")



 Chris


 

-----Original Message-----
From: James Carr <ja...@gmail.com>
To: Struts Users Mailing List <us...@struts.apache.org>
Sent: Wed, Aug 12, 2009 5:30 pm
Subject: Validating that two fields match?










Hello,

Is there something out of the box that will let me validate if two
fields match in struts2? An example would be a form with email and
confirmingEmail fields.

Thanks,
James

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




 


Re: Validating that two fields match?

Posted by Andras Balogh <an...@reea.net>.
Hi,

I have created a validator called "SameValueValidator", this worked both 
client-side and server side, this was for struts2.0.x so I don't know
how will work with the latest struts (and if it works with annotations).

If You are interested here it is:
SameValueValidator.java:
--------------------------
public class SameValueValidator extends FieldValidatorSupport
{
    private String  otherField;
    @Override
    public void validate(Object object) throws ValidationException
    {
        String fieldName = getFieldName();
        String val = (String) getFieldValue(fieldName, object);
        if (val == null || val.length() <= 0) {
            // use a required validator for these
            return;
        }
        String otherVal=(String) getFieldValue(otherField, object);
        if(!val.equals(otherVal))
        {
            addFieldError(fieldName, object);
        }
    }
    public String getOtherField()
    {
        return otherField;
    }
    public void setOtherField(String otherField)
    {
        this.otherField = otherField;
    }
}
--------------------------
How to use it in xml:
<field name="password2">
<field-validator type="samevalue" >
    <param name="otherField">login.userPasswd</param>
    <message key="error.passwordmatch" />
</field-validator>
</field>

Add it as Validator is validators.xml:
<validator name="samevalue" 
class="yourpackage.validators.SameValueValidator"/>

Changes required for client-side validation (javascript):
file: template\xhtml\form-close-validate.ftl:

(there are a bunch of elseifs, add this at the end):
-----------------------------------------
<#elseif validator.validatorType = "samevalue">
    if (field.value != null) {
        var otherField=form.elements['${validator.otherField}'];
        if (field.value!=otherField.value) {
            addError(field, error);
            errors = true;
        }
    }
</#if>
-----------------------------------------

Regards,
Andras.

James Carr wrote:
> Hello,
>
> Is there something out of the box that will let me validate if two
> fields match in struts2? An example would be a form with email and
> confirmingEmail fields.
>
> Thanks,
> James
>
> ---------------------------------------------------------------------
> 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