Results 1 to 4 of 4

Thread: not call custom function in valang "customFunctions"

Hybrid View

  1. #1
    Join Date
    Aug 2009
    Posts
    167

    Default not call custom function in valang "customFunctions"

    my spring config:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    	<bean id="profileValidator" class="org.springmodules.validation.valang.ValangValidator">
    		<property name="customFunctions">
    			<map>
    				<entry key="doIt">
    					<value>com.mycompany.myproject.domain.DoItFunction</value>
    				</entry>
    			</map>
    		</property>
    
    
    		<property name="valang">
    			<value>
    				<![CDATA[
    					{ name : ? HAS LENGTH : 'Profile name is required' : 'required.profileName' }
    					{ name : ? IS BLANK OR length(?) <= 30 : 'Profile name must be no longer than {0} characters' : 'proflieName.length': 30 }
    				]]>
    			</value>
    		</property>
    	</bean>
    
    </beans>
    my custom function :
    Code:
    public class DoItFunction extends AbstractFunction {
    	private ProfileManager profileManager;
    	private final Log logger = LogFactory.getLog(getClass());
    
    	public void setProfileManager(ProfileManager profileManager) {
    		this.profileManager = profileManager;
    	}
    
    	public DoItFunction(Function[] arguments, int line, int column) {
    		super(arguments, line, column);
    		logger.info("TEST HERE");
    		definedExactNumberOfArguments(1);
    	}
    
    	@Override
    	public boolean isAutowireByType() {
    		logger.info("TEST HERE2");
    		return true;
    	}
    
    	// The init() method will be called by Valang after the properties have been
    	// autowired
    	@Override
    	public void init() throws Exception {
    		logger.info("INIIIIIIIIIIIIIIIIII");
    		Assert.notNull(profileManager, "profileManager is requirede");
    	}
    
    	@Override
    	protected Object doGetResult(Object target) throws Exception {
    		logger.info("!!!!!!!!!!!!!target=" + target);
    		return null;
    	}
    
    }
    But when I enter profile name and click submit call only "valang" (check empty and length), but my custom function "DoItFunction" not call.

  2. #2
    Join Date
    Aug 2009
    Posts
    167

    Default

    I solved task by this way:

    my spring config:
    Code:
    	<bean id="profileValidator" class="com.mycompany.myproject.domain.ProfileValidator">
    		<property name="profileManager" ref="profileManager" />
    		<property name="valang">
    			<value>
    				<![CDATA[
    					{ name : ? HAS LENGTH : 'Profile name is required' : 'required.profileName' }
    					{ name : ? IS BLANK OR length(?) <= 30 : 'Profile name must be no longer than {0} characters' : 'proflieName.length': 30 }
    				]]>
    			</value>
    		</property>
    	</bean>
    my custom controller:
    Code:
     import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.validation.Errors;
    import org.springmodules.validation.valang.ValangValidator;
    
    public class ProfileValidator extends ValangValidator {
    	private ProfileManager profileManager;
    	private final Log logger = LogFactory.getLog(getClass());
    
    	public void setProfileManager(ProfileManager profileManager) {
    		this.profileManager = profileManager;
    	}
    
    	@Override
    	public void validate(Object target, Errors errors) {
    		Profile profile = (Profile) target;
    		if (profileManager.isExist(profile)) {
    			errors.rejectValue("name", "exist.profileName",
    					"Profile name already exist");
    		}
    		super.validate(target, errors);
    	}
    }
    It's call my custom method. Is this a good solution?

  3. #3
    Join Date
    Aug 2009
    Posts
    167

    Default

    But have one problem. It doesn't matter if "valang" controls return true or false (empty string or length) the my custom method ProfileValidator.validate(...) is ALWAYS call. But I want to call ProfileValidator.validate(...) ONLY when first two controls (empty string and length) return true.
    Is this a possible?

  4. #4
    Join Date
    Aug 2009
    Posts
    167

    Default

    I found the error. I forgot to call my custom function. Here work code:
    Code:
     	<bean id="profileValidator" class="org.springmodules.validation.valang.ValangValidator">
    		<property name="customFunctions">
    			<map>
    				<entry key="profileValidator"
    					value="com.mycompany.myproject.domain.validators.ProfileValidatorFunction" />
    			</map>
    		</property>
    		<property name="valang">
    			<value>
    				<![CDATA[
    					{ name : ? HAS LENGTH : 'Profile name is required' : 'required.profileName' }
    					{ name : ? IS BLANK OR length(?) <= 30 : 'Profile name must be no longer than {0} characters' : 'proflieName.length': 30 }
    					{ name : profileValidator(?) = TRUE : 'Profile name already exist' : 'exist.profileName' }
    				]]>
    			</value>
    		</property>
    	</bean>

Posting Permissions

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