Results 1 to 2 of 2

Thread: Valang Custom Functions

  1. #1
    Join Date
    Oct 2005
    Location
    Edmonton, AB
    Posts
    40

    Default Valang Custom Functions

    Hello,

    I'm trying to create a custom function for use with valang validation.

    I have the following function:

    package com.myProject.validation;

    import java.util.regex.Pattern;

    import org.springmodules.validation.functions.AbstractFun ction;
    import org.springmodules.validation.functions.Function;

    public class EmailValidatorFunction extends AbstractFunction {

    public EmailValidatorFunction(Function[] arg0, int arg1, int arg2) {
    super(arg0, arg1, arg2);
    definedExactNumberOfArguments(1);
    }

    @Override
    protected Object doGetResult(Object target) throws Exception {
    Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
    String str = getArguments()[0].getResult(target).toString();
    return pattern.matcher(str.subSequence(0, str.length())).matches() ? Boolean.TRUE
    : Boolean.FALSE;
    }

    }


    With the following bean definition:
    <bean id="emailValidator
    <property name="customFunctions">
    <map>
    <entry key="emailValidator"
    value="com.myProject.validation.EmailValidatorFunc tion">
    <value>
    <![CDATA[
    { email : emailValidator(email) : 'Email is not formatted correctly.' }
    ]]>
    </value>
    </entry>
    </map>
    </property>
    </bean>


    this fails miserably: <entry> is only allowed to contain either a 'value' attribute OR a 'value-ref' attribute OR a value sub-element

    AND if I'm to define my bean the following way:
    <bean id="emailValidator
    <property name="customFunctions">
    <map>
    <entry key="emailValidator"> <value>com.myProject.validation.EmailValidatorFunc tion</value>
    </entry>
    </map>
    </property>
    </bean>

    I get a class not found exception on com.myProject.validation.EmailValidatorFuntion

    How does the function definition work?

  2. #2
    Join Date
    Oct 2005
    Posts
    2

    Default example

    You have to add the property : valang
    <property name="valang">

    Code:
    <beans>
    	<bean id="testCustomFunctions" class="org.springmodules.validation.ValangValidatorFactoryBean" lazy-init="true">
    		<property name="customFunctions">
    			<map>
    				<entry key="tupper">
    					<value>org.springmodules.validation.functions.UpperCaseFunction</value>
    				</entry>
    				<entry key="lifeCycle" value="org.springmodules.validation.LifeCycleTestFunction"/>
    			</map>
    		</property>
    		<property name="valang">
    			<value><![CDATA[
    { firstName : length(tupper(?)) > 0 : 'First name is empty' }
    { lifeCycleBean : lifeCycle(?) is not null and length('abc\'') = 4 and length('\\') = 1 : 'Life cycle property is null' }
    			]]></value>
    		</property>
    	</bean>
    
    	<bean class="java.util.Date"/>
    	
    	<bean id="pattern" class="java.util.regex.Pattern" factory-method="compile">
    		<constructor-arg value="foo|bar"/>
    	</bean>
    	
    </beans>

Posting Permissions

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