<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "Validation is dynamic"]]></title>
		<link>http://book.mentaframework.org/posts/list/6.page</link>
		<description><![CDATA[Latest messages posted in the topic "Validation is dynamic"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Validation is dynamic</title>
				<description><![CDATA[ It is important to realize that the validation code inside the [i]prepareValidator()[/i] method is executed for each request. Therefore you can dynamically perform your validation based on the action parameters and characteristics. Check the example below:<br /> [code]<br /> public class MyAction extends BaseAction implements Validatable {<br />     <br />     public String execute() {<br />         <br />         // your action stuff here...<br />         <br />     }<br />     <br />     public void prepareValidator(Validator val, String innerAction) {<br /> <br />             if (isPost()) {<br />         <br />                 Rule required = RequiredFieldRule.getInstance();<br />         <br />                 val.add("username", required, FIELD_REQUIRED_ERROR);<br />                 val.add("username", StringRule.getInstance(6, 30), INVALID_USERNAME_LENGTH);<br />         <br />                 val.add("age", required, FIELD_REQUIRED_ERROR);<br />                 val.add("age", IntegerRule.getInstance(18, 50), INVALID_AGE);<br />         <br />                 val.add("password", required, FIELD_REQUIRED_ERROR);<br />                 val.add("password", StringRule.getInstance(4, 20), INVALID_PASSWORD_LENGTH);<br />                 val.add("password", EqualRule.getInstance("password", "passconf"), PASSWORD_DOES_NOT_MATCH);<br />         <br />                 val.add("passconf", required, FIELD_REQUIRED_ERROR);<br />             }    <br />         }<br />     <br />     private static final String FIELD_REQUIRED_ERROR = "Field required!";<br />     private static final String INVALID_USERNAME_LENGTH = "Username length invalid!";<br />     private static final String INVALID_AGE = "Invalid age!";<br />     private static final String INVALID_PASSWORD_LENGTH = "Invalide password length!";<br />     private static final String PASSWORD_DOES_NOT_MATCH = "Passwords do not match!";<br /> }<br /> [/code]<br /> In the example above, only a POST request will be validated. A GET will not have any validation. You can also perform validation based on the inner action, which is very common as each inner action will have its own validation. Check below:<br /> [code]public class MyAction extends BaseAction implements Validatable {<br />     <br />     public String execute() {<br />         <br />         // your action stuff here...<br />         <br />     }<br />     <br />     public void prepareValidator(Validator val, String innerAction) {<br /> <br />             if (innerAction != null && innerAction.equals("addUser")) {<br />         <br />                 Rule required = RequiredRule.getInstance();<br />         <br />                 val.add("username", required, FIELD_REQUIRED_ERROR);<br />                 val.add("username", StringRule.getInstance(6, 30), INVALID_USERNAME_LENGTH);<br />         <br />                 val.add("age", required, FIELD_REQUIRED_ERROR);<br />                 val.add("age", IntegerRule.getInstance(18, 50), INVALID_AGE);<br />         <br />                 val.add("password", required, FIELD_REQUIRED_ERROR);<br />                 val.add("password", StringRule.getInstance(4, 20), INVALID_PASSWORD_LENGTH);<br />                 val.add("password", EqualRule.getInstance("password", "passconf"), PASSWORD_DOES_NOT_MATCH);<br />         <br />                 val.add("passconf", required, FIELD_REQUIRED_ERROR);<br />             }    <br />         }<br />     <br />     private static final String FIELD_REQUIRED_ERROR = "Field required!";<br />     private static final String INVALID_USERNAME_LENGTH = "Username length invalid!";<br />     private static final String INVALID_AGE = "Invalid age!";<br />     private static final String INVALID_PASSWORD_LENGTH = "Invalide password length!";<br />     private static final String PASSWORD_DOES_NOT_MATCH = "Passwords do not match!";<br /> }<br /> [/code]<br /> In the code above, only the "addUser" innerAction will be validated. All others, including the default (no inner action) [i]execute()[/i] method will not be validated. Recall that [i]innerAction == null[/i] means the [i]execute()[/i] method was called.<br /> <br /> Because of the dynamic characteristic of the validation, you should avoid creating new rule objects on each request. That's why we have been using the [i]getInstance()[/i] method instead of the [i]new [/i]constructor for our rules. The [i]getInstance()[/i] method returns the same instance every time, avoiding the creation of new objects on every request. Despite the fact that the Java garbage collector is very powerful nowadays, you should try to avoid creating too many Java objects unnecessarily.<br /> <br /> Most of the Mentawai built-in rules already provide the [i]getInstance()[/i] method. If possible, and as a good practice, you should provide this method with any new rule you code. We will see how to create new validation rules in a bit.]]></description>
				<guid isPermaLink="true">http://book.mentaframework.org/posts/preList/25/26.page</guid>
				<link>http://book.mentaframework.org/posts/preList/25/26.page</link>
				<pubDate><![CDATA[Sat, 9 Aug 2008 08:44:17]]> GMT</pubDate>
				<author><![CDATA[ saoj]]></author>
			</item>
	</channel>
</rss>
