<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "Validation through a separate filter"]]></title>
		<link>http://recipes.mentaframework.org/posts/list/2.page</link>
		<description><![CDATA[Latest messages posted in the topic "Validation through a separate filter"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Validation through a separate filter</title>
				<description><![CDATA[ [code]<br /> <br /> public class HelloValidator extends ValidationFilter {<br /> 	<br />     public void prepareValidator(Validator val, Action action, String innerAction) {<br />         <br />             Rule required = RequiredFieldRule.getInstance();<br />         <br />             val.add("username", required, "required_error");<br />             val.add("username", StringRule.getInstance(6, 30), "invalid_username");<br />         <br />             val.add("age", required, "required_error");<br />             val.add("age", IntegerRule.getInstance(18, 50), "invalid_age");<br />         <br />             val.add("password", required, "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, "required_error");<br />     }<br /> }<br /> [/code]<br /> <br /> And the internationalized messages would be inside the /validation/HelloValidator_[i]loc[/i].i18n file, where [i]loc [/i]is the locale (ex: HelloValidator_en_US.i18n). OBS: If you are using a master i18n file through [i]LocaleManager.useMasterI18N(true)[/i], then your messages will be inside the /i18n/master_en_US.i18n.<br /> [code]<br /> ###############################################<br /> # Messages for the filter HelloValidator #<br /> ###############################################<br />  <br /> required_error = Required field cannot be left blank<br /> invalid_username = Your username must be between %min% and %max% characters long.<br /> invalid_age = You must be %min% years old or older.<br /> invalid_password_length = Your password must be between %min% and %max% characters long.<br /> password_does_not_match = Passwords do not match.<br /> [/code]<br /> Now if you don't want to put your messages in a separate file, then you can just hardcode your messages:<br /> [code]<br /> public class HelloValidator extends ValidationFilter {<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 />     public void prepareValidator(Validator val, Action action, String innerAction) {<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 /> [/code]<br /> Now to display your message in the JSP, you can use the menta tag. You can choose from mtw:hasError and mtw:outError tag.<br /> [code]<br /> &lt;%@ taglib uri="/WEB-INF/lib/mentawai.jar" prefix="mtw" %&gt;<br /> &lt;!--<br />  Note: The form loses all data on each submition!<br />  Mentawai can solve this problem automatically for you through its HTML tags.<br />  --&gt;<br /> <br /> &lt;html&gt;<br /> &lt;body&gt;<br /> &lt;h1&gt;Hello Validation!&lt;/h1&gt;<br /> <br /> &lt;mtw:form&gt;<br /> <br /> Your username: &lt;mtw:input name="username" size="25" /&gt;<br /> <br /> &lt;mtw:hasError&gt;<br /> 	&lt;font color="red"&gt;&lt;mtw:error field="username" /&gt;&lt;/font&gt;<br /> &lt;/mtw:hasError&gt;<br /> <br /> &lt;br&gt;Your age: &lt;mtw:select name="age" size="1" list="ages"&gt;<br /> <br /> &lt;mtw:hasError&gt;<br /> 	&lt;font color="red"&gt;&lt;mtw:error field="age" /&gt;&lt;/font&gt;<br /> &lt;/mtw:hasError&gt;<br /> <br /> &lt;br&gt;Your password: &lt;mtw:input type="password" name="password" size="25" /&gt;<br /> <br /> &lt;mtw:outError field="password" &gt;<br /> 	&lt;font color="red"&gt;&lt;mtw:out /&gt;&lt;/font&gt;<br /> &lt;/mtw:outError&gt;<br /> <br /> &lt;br&gt;Again please: &lt;mtw:input type="password" name="passconf" size="25" /&gt;<br /> <br /> &lt;mtw:outError field="passconf"&gt;<br /> 	&lt;font color="red"&gt;&lt;mtw:out /&gt;&lt;/font&gt;<br /> &lt;/mtw:outError&gt;<br /> <br /> &lt;mtw:submit value="Enviar" action="/HelloWorld.mtw" method="post" /&gt;<br /> <br /> &lt;/mtw:form&gt;<br /> &lt;/body&gt;<br /> &lt;/html&gt;<br /> [/code]<br /> Note that this tags are conditional tags, in other words, nothing will be shown if there is no error for the field.<br /> <br /> To add the HelloValidator as an action specific filter inside your application manager, you can just do this:<br /> [code]<br /> @Override<br /> public void loadActions() {<br /> <br />     action(HelloAction.class)<br />         .filter(new HelloValidator());<br /> <br />     // OR<br /> <br />     ActionConfig ac = new ActionConfig(HelloAction.class);<br />     ac.addFilter(new HelloValidator());<br />     addActionConfig(ac);<br /> }<br /> [/code]<br /> [color=red][b]TIP:[/b][/color] Use the Mentawai HTML form tags mtw:input, mtw:select, etc. so you don't have to worry about re-displaying the values that the user typed in case of validation error. We know you don't want to place a bunch of scriptlets in your JSPs to do this.]]></description>
				<guid isPermaLink="true">http://recipes.mentaframework.org/posts/preList/3/3.page</guid>
				<link>http://recipes.mentaframework.org/posts/preList/3/3.page</link>
				<pubDate><![CDATA[Thu, 10 Jan 2008 08:54:26]]> GMT</pubDate>
				<author><![CDATA[ saoj]]></author>
			</item>
	</channel>
</rss>
