<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "Placing messages in the code"]]></title>
		<link>http://book.mentaframework.org/posts/list/6.page</link>
		<description><![CDATA[Latest messages posted in the topic "Placing messages in the code"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Placing messages in the code</title>
				<description><![CDATA[ Although not a good practice, you are free to place your validation error messages inside your source code instead of using an i18n file. If you application is a simple one that does not plan to use internationalization you may prefer this approach.<br /> <br /> If Mentawai does not find the i18n file or the key inside the i18n file, it will just display the key as the error message. So just place your error message as the key and you are ready to go. See the example below:<br /> [code]<br /> public class MyAction implements Validatable {<br />     <br />     public String execute() throws Exception {<br />         <br />         // your action stuff here...<br />         <br />     }<br />     <br />     public void prepareValidator(Validator val, String innerAction) {<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"), <br />                                                                            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 /> Note that we are just using the error message as the key. Mentawai executes the following logic to find the error message.<br /> <br /> :arrow: Look for the i18n file. If you don't find the i18n file, use the key as the error message.<br /> :arrow: If you found the i18n file, look for the key. If you don't find the key, use the key as the error message.<br /> :arrow: If you found the key, print the associated text as the error message.<br /> <br /> As you can see, it is very easy to place your error messages inside your source code instead of using an i18n file.]]></description>
				<guid isPermaLink="true">http://book.mentaframework.org/posts/preList/24/25.page</guid>
				<link>http://book.mentaframework.org/posts/preList/24/25.page</link>
				<pubDate><![CDATA[Sat, 9 Aug 2008 08:31:52]]> GMT</pubDate>
				<author><![CDATA[ saoj]]></author>
			</item>
	</channel>
</rss>
