Ok, let's take a look in my pojo Lbuibp, which mean Reporter Bank, below is some code sniplet from it:
/**
* @spring.validator type="required" type="mask" msgkey="errors.char9"
* @spring.validator-var name="mask" value="${char9}"
*/
public void setId(String id) {
this.id = id;
}
/**
* @spring.validator type="mask" msgkey="errors.number8"
* @spring.validator-var name="mask" value="${number8}"
*/
public void setKursLaporan(Long kursLaporan) {
this.kursLaporan = kursLaporan;
}
/**
* @spring.validator type="required"
* @spring.validator type="mask" msgkey="errors.varchar30"
* @spring.validator-var name="mask" value="${varchar30}"
*/
public void setNamaBank(String namaBank) {
this.namaBank = namaBank;
}
Ok, those are setters of my attribute: id, kursLaporan, and namaBank, and their type is easily known which is char9, number8, and varchar30.
And then I add my own validator in validation-global.xml:
<constant>
<constant-name>char9</constant-name>
<constant-value>^[a-zA-Z0-9]{9}$</constant-value>
</constant>
<constant>
<constant-name>number8</constant-name>
<constant-value>^\d{1,8}$|^\d{1,2}((\,)?\d{3}){1,2}$</constant-value>
</constant>
<constant>
<constant-name>varchar30</constant-name>
<constant-value>^[\w\s.+*(),=:'"]{1,30}$</constant-value>
</constant>
- char9, it receives nine digits alphanumeric characters
- number8, ^\d{1,8}$ which receives number from 1 until 8 characters, or ^\d{1,2}((\,)?\d{3}){1,2}$ which receives 1 or 2 numbers, and then comma (or not), and then three numbers, comma and three number as a group, which is 1 or 2 of that group
- varchar30, it receives 1 or until 30 characters of set of word character, space, dot, plus, star, and etc characters.
And then in the action-servlet.xml we add a line in the lbuibpFormController bean which control the form of Lbuibp.
<bean id="lbuibpFormController" class="...webapp.action.LbuibpFormController">
...
<property name="validator"><ref bean="beanValidator"></ref>
...
</property>
And in the applicationResources.properties we add some message as below:
errors.char9={0} is invalid, it must 9 characters of string.
errors.number8={0} is invalid, it must 8 characters of number.
errors.varchar30={0} is invalid, it must maximum 30 characters of string.
And those error messages simply called if the user is inputing unreceivable syntax that will unpass of the beanValidator.
And this beanValidator is server side validation, we can add client side validation using javascript.
No comments:
Post a Comment