java - Struts2 regex validation issue with negative numbers -
all, i'm attempting write should simple validation struts2 action. have field must contain 0 or positive integer, i'm attempting use struts2 built in regex validator accomplish this. regex i'm using '^\d*$', , i've tested outside of struts2 , believe should meet needs. (e.g. matches against '23', not 'abc' or '-5').
however, when use regex pattern in struts2, fails give me validation error negative numbers.
here's struts2 validation xml snippet:
<field name="editvo.capability.operationalqty"> <field-validator type="regex"> <param name="expression"><![cdata[^\d*$]]></param> <message key="errors.number"/> </field-validator> </field>
here of results i'm seeing when unit testing validation:
input validation passes? expected result? 23 yes yes abc no yes -5 yes no %5 no yes 5- no yes a5 no yes
as can see results above, when run unit test (or test through app), error message (as expected) when enter 'abc' or '5-', '-5' not trigger validation failure. have no idea why it's allowing '-' character through if it's first character.
i'd appreciate struts2 related regex tips; please note believe error here somehow related struts2 , how handles regex, , not regex-only issue. (fwiw - i'm getting same issue when try pattern too: '^[0-9]*$').
<field-validator type="regex">
this field validator, requires field of action set work, not work against request. it's functionality provided validation interceptor.
for confirmation of see source (it isn't bad):
the validation interceptor defined in struts-default.xml such:
<interceptor name="validation" class="org.apache.struts2.interceptor.validation.annotationvalidationinterceptor"/>
org.apache.struts2.interceptor.validation.annotationvalidationinterceptor
the annotationvalidationinterceptor extends com.opensymphony.xwork2.validator.validationinterceptor
it opening javadoc validationinterceptor following:
/** * <!-- start snippet: description --> * * interceptor runs action through standard validation framework, in turn checks action against * validation rules (found in files such <i>actionclass-validation.xml</i>) , adds field-level , action-level * error messages (provided action implements {@link com.opensymphony.xwork2.validationaware}). interceptor * 1 of last (or second last) interceptors applied in stack, assumes values have * been set on action.
and pretty sums up.
i think test cases explained type conversion alone. suspect params removing field error, not make sense validation errors kept if field value has been reset.
in conclusion: there nothing wrong struts2 regex validation, @ least when used in intended way.
Comments
Post a Comment