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-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to