开发者

Struts2 regex validation issue with negative numbers

开发者 https://www.devze.com 2023-03-22 06:00 出处:网络
All, I\'m attempting to write what should be simple validation for a Struts2 action.I have a field that must contain 0 or a positive integer, so I\'m attempting to use the Struts2 built in regex valid

All, I'm attempting to write what should be simple validation for a Struts2 action. I have a field that must contain 0 or a positive integer, so I'm attempting to use the Struts2 built in regex validator to accomplish this. The regex that I'm using is '^\d*$', and I've tested that outside of struts2 and believe it should meet my needs. (e.g. it matches against '23', but not 'abc' or '-5').

However, when I use that regex pattern in Struts2, it fails to give me a validation error for negative numbers.

Here's my struts2 valida开发者_高级运维tion 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 are some of the results I'm seeing when unit testing this 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 you can see from the results above, when I run my unit test (or test through the app), I get an error message (as expected) when I enter 'abc' or '5-', but '-5' does NOT trigger a validation failure. I have no idea why it's allowing the '-' character through if it's the first character.

I'd appreciate any Struts2 related regex tips; please note that I believe the error here is somehow related specifically to Struts2 and how it handles regex, and not simply a regex-only issue. (fwiw - I'm getting the same issue when I try this pattern too: '^[0-9]*$').


<field-validator type="regex">

This is a field validator, it requires that the field of the action already be set for it to work, it will not work against the request. It's functionality is provided by the validation interceptor.

For confirmation of this see the source (It really isn't that bad):

The validation interceptor is defined in struts-default.xml as 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 is from the opening javadoc for ValidationInterceptor we get the following:

/**
 * <!-- START SNIPPET: description -->
 *
 * This interceptor runs the action through the standard validation framework, which in turn checks the action against
 * any validation rules (found in files such as <i>ActionClass-validation.xml</i>) and adds field-level and action-level
 * error messages (provided that the action implements {@link com.opensymphony.xwork2.ValidationAware}). This interceptor
 * is often one of the last (or second to last) interceptors applied in a stack, as it assumes that all values have
 * already been set on the action.

And that pretty much sums it up.

I think your test cases are explained by type conversion alone. I suspect that params is removing the field error, it would not make any sense for validation errors to be kept if the field value has been reset.

In conclusion: There is nothing wrong with Struts2 regex validation, at least when used in the intended way.

0

精彩评论

暂无评论...
验证码 换一张
取 消