① stringlength validator
整体说明:stringlength是用来验证一个string field的长度的,通过属性fieldName指定要验证的string field;如果指定了minLength和maxLength,那么这个string field的Length就在minLength~maxLength之间;trim属性是调用Stirng.trim()来除去this string field前后的空格的,如果未指定这个属性,this string field依然会被trim()。
Example:
<validators> <!-- Plain Validator Syntax --> <validator type="stringlength"> <param name="fieldName">myPurchaseCode</param> <param name="minLength">10</param> <param name="maxLength">10</param> <param name="trim">true</param> <message>Your purchase code needs to be 10 characters long</message> </validator> <!-- Field Validator Syntax --> <field name="myPurchaseCode"> <param name="minLength">10</param> <param name="maxLength>10</param> <param name="trim">true</param> <message>Your purchase code needs to be 10 characters long</message> </field-name> </validators>
② requiredstring validator
整体说明:用来验证一个string field是否为空。通过属性fieldName指定要验证的string field; trim属性是调用Stirng.trim()来除去this string field前后的空格的,如果未指定这个属性,this string field依然会被trim()。
Example:
<validators> <!-- Plain-Validator Syntax --> <validator type="requiredstring"> <param name="fieldName">username</param> <param name="trim">true</param> <message>username is required</message> </validator> <!-- Field-Validator Syntax --> <field name="username"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>username is required</message> </field-validator> </field> </validators>
③ email validator
整体说明:检查一个给定的string field是否为空,是否是一个合法的email;
Example:
<!-- Plain Validator Syntax --> <validators> <validator type="email"> <param name="fieldName">myEmail</param> <message>Must provide a valid email</message> </validator> </validators> <!-- Field Validator Syntax --> <field name="myEmail"> <field-validator type="email"> <message>Must provide a valid email</message> </field-validator> </field>
④ date validator
整体说明:检查给定的日期是否在一个日期域内,需要注意的是:如果没有给定日期转换格式,XWorkBasicConverter会按照Date.SHORT去格式化日期,如果转换出现问题它会获取系统当前日期。
Example:
<validators> <!-- Plain Validator syntax --> <validator type="date"> <param name="fieldName">birthday</param> <param name="min">01/01/1990</param> <param name="max">01/01/2000</param> <message>Birthday must be within ${min} and ${max}</message> </validator> <!-- Field Validator Syntax --> <field name="birthday"> <field-validator type="date"> <param name="min">01/01/1990</param> <param name="max">01/01/2000</param> <message>Birthday must be within ${min} and ${max}</message> </field> </field> </validators>