You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Marsman <ma...@gmx.de> on 2009/11/28 15:04:11 UTC

S2 Strange behaviour of validation

Hi!

I'm using the Struts 2 validation for my form fields. For a required numeric
input field, I've set the "required" and the "int" validator and a custom
message for each of them. But when the user enters none numeric characters,
the custom localized required-messages and a default "Invalid field value"
message are displayed in the error box. Also a NoSuchMethodException occurs
because Struts tries to store the alphanumeric field value in an action
class property of type string.

Why is a the field marked as required, when the user has entered a wrong,
but has entered a value?

Why is a default message instead of my custom message shown?

And why is there is Struts trying to store the value in a bean property, if
the value is identified as invalid?

Here is my validation:

  <field name="myNumber">
    <field-validator type="required">
      <message key="addEntry.error.myNumber.required" />
    </field-validator>
    <field-validator type="int">
      <message key="addEntry.error.myNumber.format" />
    </field-validator>
  </field>

I'm using Struts 2.1.8.1. The valiidateion XML is assigned to the action
class. The action class implements ModelDriven but myNumber is outside the
model class in the of type Integer.

Thanks for any hints or comments.

Titus

-- 
View this message in context: http://old.nabble.com/S2-Strange-behaviour-of-validation-tp26551371p26551371.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: S2 Strange behaviour of validation

Posted by Marsman <ma...@gmx.de>.
In my Struts 2 book, I've now read all about built-in type conversion and
overriding default messages. So I got a useful message now, when the user
enters an alphanumeric value in a field with an underlying int property. But
after the conversion error, Struts goes on validating and the message for a
missing required field is still displayed. How can I avoid field validation
after conversion errors?

Titus



dcabasson wrote:
> 
> Hi Titus,
> 
> This behaviour is the "standard" behaviour of struts 2. Your second 
> message comes from the conversion error : 
> http://struts.apache.org/2.1.8.1/docs/conversion-error-interceptor.html
> 
> 

-- 
View this message in context: http://old.nabble.com/S2-Strange-behaviour-of-validation-tp26551371p26581540.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: S2 Strange behaviour of validation

Posted by Denis Cabasson <de...@gmail.com>.
Hi Titus,

This behaviour is the "standard" behaviour of struts 2. Your second 
message comes from the conversion error : 
http://struts.apache.org/2.1.8.1/docs/conversion-error-interceptor.html

I suppose that your field is of type int (or any number like class). 
Struts will in the first try to convert the parameter value to stick it 
in your variable. Obviously, that won't work ("abc" isn't much of an 
int). Because if that, struts will output a stack trace in the console 
as warning (it is just for information, not an actual error). And will 
add the conversion error to the list of action errors (see interceptor 
above).

Only after that does your validation kicks in, and of course, at that 
point your value is empty, thus trigerring the required validation.

So, the bottom line is an int validator on a numeric field, when the 
type of this attribute is int is useless as it will never be reached. 
Either change your field to be a String, in which case all validation 
and conversion is up to you, or you will have to dig in Struts 2 to 
handle the conversion error a little more graciously.

Good luck!
Denis.

Marsman a écrit :
> Hi!
>
> I'm using the Struts 2 validation for my form fields. For a required numeric
> input field, I've set the "required" and the "int" validator and a custom
> message for each of them. But when the user enters none numeric characters,
> the custom localized required-messages and a default "Invalid field value"
> message are displayed in the error box. Also a NoSuchMethodException occurs
> because Struts tries to store the alphanumeric field value in an action
> class property of type string.
>
> Why is a the field marked as required, when the user has entered a wrong,
> but has entered a value?
>
> Why is a default message instead of my custom message shown?
>
> And why is there is Struts trying to store the value in a bean property, if
> the value is identified as invalid?
>
> Here is my validation:
>
>   <field name="myNumber">
>     <field-validator type="required">
>       <message key="addEntry.error.myNumber.required" />
>     </field-validator>
>     <field-validator type="int">
>       <message key="addEntry.error.myNumber.format" />
>     </field-validator>
>   </field>
>
> I'm using Struts 2.1.8.1. The valiidateion XML is assigned to the action
> class. The action class implements ModelDriven but myNumber is outside the
> model class in the of type Integer.
>
> Thanks for any hints or comments.
>
> Titus
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org