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 Clark <jc...@bsd.uchicago.edu> on 2003/05/21 19:59:33 UTC

Custom Validation gets NoSuchMethodException

I'm using Validator with Struts 1.1 RC1, trying to implement the
custom validation for comparing fields to verify they are
identical. When I attempt to use the validateTwoFields method I get a
NoSuchMethodException.

I've basically copied and pasted the code for the various files into
my app, as described in the Validator guide and
http://www.raibledesigns.com/page/rd/20030226. The other validation
classes (non-custom, e.g. required, mask) work just fine.

There have been several questions about this on the list but no
answers.

The only possible clue that I have is that before I explicity named the
methodParams in the validation.xml file, reflection had the
wrong signature for the "validateTwoFields" method --it was missing
some arguments, namely ActionErrors errors, HttpServletRequest
request, ServletContext application. This may mean nothing. The
signature seems to be correct now.

My guess is that there is some mismatch in versions between validation
and/or struts classes. So I downloaded a fresh RC1 and copied all the
lib/*.jar files into my app. No difference. I've searched for stray
jar files that could be finding their way into classpaths. I've
recompiled my ValidationUtil class with variations of the signature. Nada.

This seems straightforward--no doubt I'm overlooking something
simple. Relevant snippets from key files follow.

TIA,
jc



CATALINA.OUT (the error)
------------------------------------

May 20, 2003 8:00:59 PM org.apache.commons.validator.Validator 
validateFieldForRule
SEVERE: reflection: 
amcas.ValidationUtil.validateTwoFields(java.lang.Object, 
org.apache.commons.validator.ValidatorAction, 
org.apache.commons.validator.Field, 
org.apache.struts.action.ActionErrors, 
javax.servlet.http.HttpServletRequest)
java.lang.NoSuchMethodException: 
amcas.ValidationUtil.validateTwoFields(java.lang.Object, 
org.apache.commons.validator.ValidatorAction, 
org.apache.commons.validator.Field, 
org.apache.struts.action.ActionErrors, 
javax.servlet.http.HttpServletRequest)
         at java.lang.Class.getMethod0(Class.java:1756)
         at java.lang.Class.getMethod(Class.java:963)
         at 
org.apache.commons.validator.Validator.validateFieldForRule(Validator.java:393)
         at 
org.apache.commons.validator.Validator.validateField(Validator.java:512)
         at 
org.apache.commons.validator.Validator.validate(Validator.java:551)
         at 
org.apache.struts.validator.DynaValidatorActionForm.validate(DynaValidatorActionForm.java:122)
         at 
org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:937)
...



VALIDATOR-RULES.XML
------------------------------------

<validator name="twofields"
     classname="amcas.ValidationUtil" method="validateTwoFields"
     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.identical">
     <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
------------------------------------
         <!-- DynaValidatorActionForm uses path not actual name -->
         <form name="/id">

          <field    property="amcasId1"
                    depends="required,mask,minlength,twofields">
                      <arg0 key="id.amcasId1"/>
                      <arg1 name="minlength" key="${var:minlength}" 
resource="false"/>
                      <var>
                        <var-name>mask</var-name>
                        <var-value>^\w+$</var-value>
                      </var>
                      <var>
                        <var-name>minlength</var-name>
                        <var-value>9</var-value>
                        </var>
                    <var>
                        <var-name>secondProperty</var-name>
                        <var-value>amcasId2</var-value>
                    </var>
          </field>

    </formset>




STRUTS-CONFIG.XML
------------------------------------
     <action    path="/id"
                type="amcas.IdAction"
                scope="session"
                name="applicationForm"
                validate="true"
                input="id">
     </action>
...

   <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
     <set-property property="pathnames"
                      value="/WEB-INF/validator-rules.xml,
                             /WEB-INF/validation.xml"/>
   </plug-in>




BUILD.XML (used to compile the ValidationUtil class)
------------------------------------
     <path id="compile.classpath">
         <pathelement path ="lib/commons-beanutils.jar"/>
         <pathelement path ="lib/commons-digester.jar"/>
         <pathelement path ="lib/struts.jar"/>
         <pathelement path ="${servlet.jar}"/>
         <pathelement path ="${jdbc20ext.jar}"/>
         <pathelement path ="lib/commons-validator.jar"/>
         <pathelement path ="classes"/>
         <pathelement path ="${classpath}"/>
     </path>




ValidationUtil class
------------------------------------
package amcas;

import java.io.Serializable;
import java.lang.*;
import java.text.*;
import java.util.*;
import org.apache.struts.action.*;
import org.apache.struts.validator.Resources;
import org.apache.commons.validator.*;
import org.apache.commons.validator.ValidatorUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletContext;

public class ValidationUtil implements Serializable {

public static boolean validateTwoFields(
         Object bean,
         ValidatorAction va,
         Field field,
         ActionErrors errors,
         HttpServletRequest request,
         ServletContext application) {

         String value = ValidatorUtil.getValueAsString(
             bean,
             field.getProperty());
         String sProperty2 = field.getVarValue("secondProperty");
         String value2 = ValidatorUtil.getValueAsString(
             bean,
             sProperty2);

         if (!GenericValidator.isBlankOrNull(value)) {
            try {
               if (!value.equals(value2)) {
                  errors.add(field.getKey(),
                          Resources.getActionError(request, va, field));

                  return false;
               }
            } catch (Exception e) {
                  errors.add(field.getKey(),
                          Resources.getActionError(request, va, field));
                  return false;
            }
         }

         return true;
     }
}



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