Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
not call custom function in valang "customFunctions" Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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?

    Comment


    • #3
      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?

      Comment


      • #4
        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>

        Comment

        Working...
        X