Now, server-side validation works!
I changed ActionErrors class with ActionMessages class into my java
validation method and into "validator-rules.xml".
The only problem is that
Resources.getActionMessage(request, va, field)
method in my custom validator class is deprecated and I don't know how to
replace it.
Can you give me any suggestions?


On the client-side, I have the same problem.
I see a javascript warning in browser: "twofields is not defined".
I have also tried to perform some changes to my files:
**************
validator-rules.xml
**************
 <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD
Commons Validator Rules Configuration 1.3.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd";>
...
...
...
<validator name="twofields" 
                        
classname="it.sfidiamoci.validator.ValidatorPersonalizzati"
                        method="validateTwoFields" 
                        methodParams="java.lang.Object,
                                                
org.apache.commons.validator.ValidatorAction,
                                                
org.apache.commons.validator.Field,
                                                
org.apache.struts.action.ActionMessages,
                                                
javax.servlet.http.HttpServletRequest" 
                        depends="required"
                        msg="errors.twofields">
    <javascript><![CDATA[
        function validateTwoFields(form) {
            var bValid = true;
            var focusField = null;
            var i = 0;
            var fields = new Array();
            oTwoFields = new twofields();
            for (x in oTwoFields) {
                var field = form[oTwoFields[x][0]];
                var secondField = form[oTwoFields[x][2]("secondProperty")];
            
                if (field.type == 'text' ||
                    field.type == 'textarea' ||
                    field.type == 'select-one' ||
                    field.type == 'radio' ||
                    field.type == 'password') {
            
                    var value;
                    var secondValue;
                    // get field's value
                    if (field.type == "select-one") {
                        var si = field.selectedIndex;
                        value = field.options[si].value;
                        secondValue = secondField.options[si].value;
                    } else {
                        value = field.value;
                        secondValue = secondField.value;
                    }
                
                    if (value != secondValue) {
                    
                        if (i == 0) {
                            focusField = field;
                        }
                        fields[i++] = oTwoFields[x][1];
                        bValid = false;
                    }
                }
            }
            
            if (fields.length > 0) {
                focusField.focus();
                alert(fields.join('\n'));
            }
            
            return bValid;
        }]]></javascript>
</validator>

**********
validation.xml
**********
 <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD
Commons Validator Rules Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd";>
...
...
...
<field property="password" depends="required,minlength,mask,twofields">
                                <arg position="0" key="errors.password" />
                                <arg position="1" name="minlength" 
key="${var:minlength}"
resource="false" />
                                <msg name="mask" key="errors.mask.password" />
                                <msg name="uguale" key="errors.password.uguale" 
/>
                                <var>
                                        <var-name>minlength</var-name>
                                        <var-value>8</var-value>
                                </var>
                                <var>
                                        <var-name>mask</var-name>
                                        <var-value>^[a-zA-Z0-9_.]*$</var-value>
                                </var>
                                <var>
                                        <var-name>secondProperty</var-name>
                                        <var-value>password2</var-value>
                                </var>
                        </field>

So, my custom validation ("twofields") doesn't still work on client-side.
Can you help me?
I also tried to change the version of dtd validator...but nothing changed.
I can't understand where is the problem.




cacodemon79 wrote:
> 
> I'm using Struts 1.3.
> I'm trying to write a custom validator to check that 2 fields are
> identical.
> 
> 1) I modified my validator-rules.xml by adding my own validator (called
> "uguale"):
> **************
> validator-rules.xml
> **************
> <validator name="uguale"
>                       
> classname="it.sfidiamoci.validator.ValidatorPersonalizzati"
>                       method="validateUguale"
>               
> methodParams="java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionErrors,javax.servlet.http.HttpServletRequest"
>  
>                       depends="required"
>                       msg="errors.uguale">
>                       
> <javascript><![CDATA[
>         function validateUguale(form) {
>             var bValid = true;
>             var focusField = null;
>             var i = 0;
>             var fields = new Array();
>             oTwoFields = new twofields();
>             for (x in oTwoFields) {
>                 var field = form[oTwoFields[x][0]];
>                 var secondField =
> form[oTwoFields[x][2]("secondProperty")];
>             
>                 if (field.type == 'text' ||
>                     field.type == 'textarea' ||
>                     field.type == 'select-one' ||
>                     field.type == 'radio' ||
>                     field.type == 'password') {
>             
>                     var value;
>                     var secondValue;
>                     // get field's value
>                     if (field.type == "select-one") {
>                         var si = field.selectedIndex;
>                         value = field.options[si].value;
>                         secondValue = secondField.options[si].value;
>                     } else {
>                         value = field.value;
>                         secondValue = secondField.value;
>                     }
>                 
>                     if (value != secondValue) {
>                     
>                         if (i == 0) {
>                             focusField = field;
>                         }
>                         fields[i++] = oTwoFields[x][1];
>                         bValid = false;
>                     }
>                 }
>             }
>             
>             if (fields.length > 0) {
>                 focusField.focus();
>                 alert(fields.join('\n'));
>             }
>             
>             return bValid;
>         }]]></javascript>
> 
> </validator>
> 
> 2) I modified my validation.xml by adding the new validator (called
> "uguale"):
> **************
> validator-rules.xml
> **************
> <i<field property="password" depends="required,minlength,mask,uguale">
>                               <arg position="0" key="errors.password" />
>                               <arg position="1" name="minlength" 
> key="${var:minlength}"
> resource="false" />
>                               <msg name="mask" key="errors.mask.password" />
>                               <msg name="uguale" key="errors.password.uguale" 
> />
>                               <var>
>                                       <var-name>minlength</var-name>
>                                       <var-value>8</var-value>
>                               </var>
>                               <var>
>                                       <var-name>mask</var-name>
>                                       <var-value>^[a-zA-Z0-9_.]*$</var-value>
>                               </var>
>                               <var>
>                                       <var-name>secondProperty</var-name>
>                                       <var-value>password2</var-value>
>                               </var>
>                       </field>
> 
> 3) I added the key "errors.password.uguale" in
> ApplicationResources.properties
> ***********************
> ApplicationResources.properties
> ***********************
> errors.password.uguale=Le password inserite non sono uguali
> 
> 4) I created the class "ValidatorPersonalizzati" to handle the custom
> validator "validateUguale":
> ********************
> ValidatorPersonalizzati.java
> ********************
> package it.sfidiamoci.validator;
> 
> import javax.servlet.http.HttpServletRequest;
> 
> import org.apache.commons.validator.Field;
> import org.apache.commons.validator.GenericValidator;
> import org.apache.commons.validator.ValidatorAction;
> import org.apache.commons.validator.util.ValidatorUtils;
> import org.apache.struts.action.ActionErrors;
> import org.apache.struts.validator.Resources;
> 
> 
> public class ValidatorPersonalizzati{
>       
>       public static boolean validateUguale(Object bean, ValidatorAction
> va,Field field, ActionErrors errors,HttpServletRequest request){
>               
>               String value =
> ValidatorUtils.getValueAsString(bean,field.getProperty());
> System.out.println("value="+value);
>               String sProperty2 = field.getVarValue("secondProperty");
>               String value2 = 
> ValidatorUtils.getValueAsString(bean,sProperty2);
> System.out.println("value2="+value2);
>               if (!GenericValidator.isBlankOrNull(value)){
>                       try{
>                               if (!value.equals(value2)){
>                                       
> errors.add(field.getKey(),Resources.getActionMessage(request, va,
> field));
>                                       return false;
>                               }
>                       }
>                       catch (Exception e){
>                               
> errors.add(field.getKey(),Resources.getActionMessage(request, va,
> field));
>                               return false;
>                       }
>               }
>               
>               return true;
> 
>       }       
> }
> 
> 5) I have two fields ("password", "password2") to check in my jsp:
> ******
> test.jsp
> ******
> <tr>
>                                                       <td 
> class="col1"><bean:message
> key='registrazioneUtente.password.label'/></td>
>                                                       <td><html:password 
> property="password" styleId="password"
> maxlength="<%= passwordMaxLength %>" /></td>
>                                                       <td><bean:message 
> key='registrazioneUtente.password2.label'/></td>
>                                                       <td><html:password 
> property="password2" styleId="password2"
> maxlength="<%= passwordMaxLength %>" /></td>
>                                               </tr>
> 
> 
> When I submit the form in my jsp, standard validations (e.g. required,
> minlength, ...) of other fields are performed in the correct way.
> Instead, my own validation called "uguale" ("password==password2") doesn't
> work!
> On the client-side, I see a javascript warning in browser: "twofields is
> not defined" but no javascript alert.
> On the server-side, I get the following exception:
> 
> value=cvcxvxvczczx
> value2=czzxcczxczczczcz
> 24-giu-2008 20.54.30 org.apache.commons.validator.ValidatorAction
> executeValidationMethod
> GRAVE: Unhandled exception thrown during validation: null
> java.lang.NullPointerException
>       at
> it.sfidiamoci.validator.ValidatorPersonalizzati.validateUguale(ValidatorPersonalizzati.java:30)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>       at java.lang.reflect.Method.invoke(Unknown Source)
>       at
> org.apache.commons.validator.ValidatorAction.executeValidationMethod(ValidatorAction.java:544)
>       at org.apache.commons.validator.Field.validateForRule(Field.java:796)
>       at org.apache.commons.validator.Field.validate(Field.java:876)
>       at org.apache.commons.validator.Form.validate(Form.java:288)
>       at org.apache.commons.validator.Validator.validate(Validator.java:351)
>       at
> org.apache.struts.validator.ValidatorForm.validate(ValidatorForm.java:116)
>       at
> org.apache.struts.chain.commands.servlet.ValidateActionForm.validate(ValidateActionForm.java:58)
>       at
> org.apache.struts.chain.commands.AbstractValidateActionForm.execute(AbstractValidateActionForm.java:120)
>       at
> org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
>       at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
>       at
> org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
>       at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
>       at
> org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
>       at
> org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
>       at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
>       at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
>       at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
>       at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
>       at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>       at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
>       at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
>       at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
>       at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
>       at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
>       at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
>       at
> org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
>       at
> org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
>       at
> org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
>       at java.lang.Thread.run(Unknown Source)
> 
> In the log I have left 2 System.out.println to be sure that "value" and
> "value2" variables had the correct value submitted through the form.
> 
> Moreover, I'm not sure that 
> Resources.getActionMessage(request, va, field)
> method in my custom validator class is correct. This method is deprecated
> and I don't know how to replace it.
> 
> I can't understand where is the problem both on client-side and on
> server-side validation.
> 
> Can you help me?
> 
> Thanks in advance.
> 

-- 
View this message in context: 
http://www.nabble.com/Struts-Validator-Framework%3A-problem-with-Custom-Validator-tp18098053p18122639.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to