Results 1 to 9 of 9

Thread: problem with spring-sandbox Validation

Hybrid View

  1. #1
    Join Date
    Sep 2004
    Location
    Paris-France
    Posts
    31

    Default problem with spring-sandbox Validation

    I try this code to validate my form:

    --validation.xml--
    <form-validation>
    <formset>
    <form name="smsForm">
    <field property="text" depends="required">
    <arg0 key="smsForm.text"/>
    </field>
    </form>
    </formset>
    </form-validation>

    --sms.jsp--<form name="smsForm" method="post" action="<c:url value="/sendsms.form" />">
    <table width="75%" border="0">
    <tr>
    <td></td>
    </tr>
    <tr>
    <td><fmt:message key="smsform.msisdn"/>
    <spring:bind path="command.instances">
    <input type="text" name="${status.expression}" value="${status.value}">
    <FONT color="red">
    <B>${status.errorMessage}</B>
    </FONT>
    </spring:bind>
    </td>
    </tr>
    <tr>
    <td><fmt:message key="smsform.text"/>
    <spring:bind path="command.text">
    <textarea name="${status.expression}">${status.value}</textarea>
    <FONT color="red">
    <B>${status.errorMessage}</B>
    </FONT>
    </spring:bind>
    </td>
    </tr>
    <tr>
    <td><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    </table>
    </form>

    --spring-servlet.xml--<bean id="SendSmsController" class="com.web.SendSmsController">
    <property name="validator">
    <ref bean="beanValidator"/>
    </property>
    </bean>

    --appicationContext.xml--
    <bean id="validatorFactory" class="org.springframework.validation.commons.Vali datorFactory"
    init-method="init">
    <property name="resources">
    <list>
    <value>/WEB-INF/validator-rules.xml</value>
    <value>/WEB-INF/validation.xml</value>
    </list>
    </property>
    </bean>

    <bean id="beanValidator" class="org.springframework.validation.commons.Bean Validator">
    <property name="validatorFactory"><ref local="validatorFactory"/></property>
    </bean>



    So when i deploy the application i have error message :( :

    java.lang.IllegalArgumentException: Validator [org.springframework.validation.commons.BeanValidat or@b0f0ae] does not support command class [com.model.Sms]

    :?: :?: what is the problem????????

  2. #2
    Join Date
    Aug 2004
    Location
    Tampa, FL
    Posts
    39

    Default

    Change this tag:

    Code:
    <form name="smsForm">
    to:

    Code:
     <form name="sms">
    Spring's support for Commons Validator uses the class name for the form name.

    I am kinda wondering if there is any other way to define validations, for example, if you have more than one set of validations for a particular object. In Struts, you don't have this limitation.

  3. #3
    Join Date
    Sep 2004
    Location
    Paris-France
    Posts
    31

    Default

    :D
    think you!
    work successfully!

  4. #4
    Join Date
    Sep 2004
    Location
    Paris-France
    Posts
    31

    Default

    now i try to validate whith a regular expression,it's work with javascript validation, but the server validation don't work!

    my code is:


    Code:
    <form name="sms">
    <.......>		
    			<field property="instances" depends="required,mask">
    		        <msg name="mask" key="errors.maskmsg" />
    				<arg0 key="smsform.instances" />
    		        <var>
    					<var-name>mask</var-name>
    					<var-value>&#91;0-9&#93;+</var-value>
    				</var>
    		    </field>
    			
            </form>
    i try a simple regular expression, so only "required" validation work on the server!
    "mask" validation have no effect on the server

  5. #5
    Join Date
    Aug 2004
    Location
    Mount Joy, PA
    Posts
    34

    Default

    Quote Originally Posted by Nilesh
    I am kinda wondering if there is any other way to define validations, for example, if you have more than one set of validations for a particular object.
    There is a way to do this. Unfortunately, the documentation sucks (I wrote it, I can say that :lol: ). Look at NamedBeanValidator. You can simply configure a different one for each set of validation rules you want to use:

    springContext.xml
    Code:
    <bean id="smsValidator1" class="org.springframework.validation.commons.NamedBeanValidator">
      <property name="validatorFactory"><ref local="validatorFactory"/></property>
      <property name="beanName"><value>sms1</value></property>
    </bean>
    
    <bean id="smsValidator2" class="org.springframework.validation.commons.NamedBeanValidator">
      <property name="validatorFactory"><ref local="validatorFactory"/></property>
      <property name="beanName"><value>sms2</value></property>
    </bean>
    validation.xml
    Code:
    <form name="sms1">
      ...
    </form>
    
    <form name="sms2">
      ...
    </form>
    ~ Daniel Miller

  6. #6
    Join Date
    Aug 2004
    Location
    Tampa, FL
    Posts
    39

    Default

    dmiller - thanks, that is what I'm looking for... So the NamedBeanValidator, when used with the Validator interface, will always try to validate any object given to it using the validations specificied by its beanName property?

    h - haven't tried regexp myself, will give it a try

  7. #7
    Join Date
    Aug 2004
    Location
    Tampa, FL
    Posts
    39

    Default

    I read in the Struts Validator documentation that you need to begin your mask with ^ and end with $. So for example, I have a mask variable like this which works:

    Code:
        <var><var-name>mask</var-name><var-value>^&#91;-.-9A-Za-z&#93;*$</var-value></var>

  8. #8
    Join Date
    Sep 2004
    Location
    Paris-France
    Posts
    31

    Default

    think you nilesh, i anderstand the problem and i have the solution
    so , now, i wnat to validate in client by a regular expression and in the server by an other regular expression ,
    because in client i have a string like
    "22222,33333,22222"
    i must validate with this reg exp:
    ^[0-9]+(( *|,) *[0-9]+)*$
    in hte server part i have a property editor whitch parse a String and make a Set .
    i must validate evry Instance on this set.
    i try this code:
    --validation.xml--
    Code:
    field property="instances" depends="required,maskList">
    				<msg name="maskList" key="errors.maskListmsg"/>
    				<arg0 key="smsform.instances" />
    		        <var>
    					<var-name>maskList</var-name>
    					<var-value>^&#91;0-9&#93;+$</var-value>
    				</var>
    				
    				<var>
    					<var-name>mask</var-name>
    					<var-value^&#91;0-9&#93;+&#40;&#40; *|,&#41; *&#91;0-9&#93;+&#41;*$</var-value>
    				</var>
    				<var>
    					<var-name>champ</var-name>
    					<var-value>destination</var-value>
    				</var>
    				
                </field>
    --validator-rules.xml--
    Code:
    <validator name="maskList"
                classname="com.validation.ListFieldChecks"
                   method="validateMaskList"
             methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                           
                  depends=""
                      msg="errors.invalid">
    
             <javascript><!&#91;CDATA&#91;
                function validateMaskList&#40;form&#41; &#123;
                    //alert&#40;'1'&#41;;
    				var isValid = true;
                    var focusField = null;
                    var i = 0;
                    var fields = new Array&#40;&#41;;
                    oMasked = new maskList&#40;&#41;;
    				//alert&#40;'2'&#41;;
                    for &#40;x in oMasked&#41; &#123;
    				//alert&#40;'3'&#41;;
                        var field = form&#91;oMasked&#91;x&#93;&#91;0&#93;&#93;;
                        
                        if &#40;&#40;field.type == 'text' || 
                             field.type == 'textarea'&#41; && 
                             &#40;field.value.length > 0&#41;&#41; &#123;
                            //alert&#40;'4'&#41;;
                            if &#40;!matchPatternList&#40;field.value, oMasked&#91;x&#93;&#91;2&#93;&#40;"mask"&#41;&#41;&#41; &#123;
                                if &#40;i == 0&#41; &#123;
                                    focusField = field;
                                &#125;
                                fields&#91;i++&#93; = oMasked&#91;x&#93;&#91;1&#93;;
                                isValid = false;
                            &#125;
                        &#125;
                    &#125;
                    
                    if &#40;fields.length > 0&#41; &#123;
                       focusField.focus&#40;&#41;;
                       alert&#40;fields.join&#40;'\n'&#41;&#41;;
                    &#125;
                    return isValid;
                &#125;
    			
    			function matchPatternList&#40;value, mask&#41; &#123;
                   //alert&#40;'5 '+value&#41;;
    			   //alert&#40;mask&#41;;
    			   return mask.exec&#40;value&#41;;
                &#125;
    			&#93;&#93;>
             </javascript>
    
          </validator>
    --com.validation.ListFieldChecks.java--
    Code:
    public static boolean validateMaskList&#40;Object bean, ValidatorAction va, Field field, Errors errors&#41;&#123;
    	 	
    		String champ = field.getVarValue&#40;"champ"&#41;;
    		
    		
    		Object liste=null;
    		
    		try &#123;
    			liste = PropertyUtils.getProperty&#40;bean,field.getProperty&#40;&#41;&#41;;
    		&#125; catch &#40;IllegalAccessException e&#41; &#123;
    			// TODO Auto-generated catch block
    			e.printStackTrace&#40;&#41;;
    		&#125; catch &#40;InvocationTargetException e&#41; &#123;
    			// TODO Auto-generated catch block
    			e.printStackTrace&#40;&#41;;
    		&#125; catch &#40;NoSuchMethodException e&#41; &#123;
    			// TODO Auto-generated catch block
    			e.printStackTrace&#40;&#41;;
    		&#125;
    		Collection set= &#40;Collection&#41;liste;
    	 	
    	 	Field f1=new Field&#40;&#41;;
    	 	f1.setProperty&#40;champ&#41;;
    	 	f1.addArg&#40;field.getArg&#40;0&#41;&#41;;
    	 	Var maskList = field.getVar&#40;"maskList"&#41;;
    	 	maskList.setName&#40;"mask"&#41;;
    	 	f1.addVar&#40;maskList&#41;;
    	 	Msg msg= new Msg&#40;&#41;;
    	 	msg.setName&#40;"maskList"&#41;;
    	 	msg.setKey&#40;field.getKey&#40;&#41;&#41;;
    	 	f1.addMsg&#40;msg&#41;;
    	 	
    	 	for &#40;Iterator iter = set.iterator&#40;&#41;; iter.hasNext&#40;&#41;;&#41; &#123;
    	 		
    			Object element = &#40;Object&#41; iter.next&#40;&#41;;
    			if&#40;!FieldChecks.validateMask&#40; element,  va,  f1,  errors&#41;&#41;&#123;
    								return false;
    			&#125;
    		&#125;
    	 		return true;
    	&#125;
    now i have a propblem whit initializing the Field.

  9. #9
    Join Date
    Aug 2004
    Location
    Mount Joy, PA
    Posts
    34

    Default

    Quote Originally Posted by nilesh
    So the NamedBeanValidator, when used with the Validator interface, will always try to validate any object given to it using the validations specificied by its beanName property?
    That's correct.
    ~ Daniel Miller

Similar Threads

  1. Spring 1.2 Resource + Acegi 0.8.2 Jaas problem
    By gmansoor in forum Security
    Replies: 10
    Last Post: Feb 26th, 2007, 02:14 PM
  2. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  3. Replies: 0
    Last Post: May 27th, 2005, 09:21 AM
  4. Replies: 2
    Last Post: May 26th, 2005, 02:30 AM
  5. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •