You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Kishore Subramanian <ki...@agilesoft.com> on 2001/02/09 21:36:05 UTC

Validating that values are of the specified type

Consider the following scenario :

I have a form element "A" that can accept only integers. 
Corresponding to this property, I have a setA(int i) and getA() methods in my ActionForm.

When the user enters a non-integer value for "A" and submits it, the ActionServlet populates the form and calls the validate method on my form bean
(assuming my form is a validating type).

I want to validate in my ActionForm if the value entered is an Integer and if not i want to populate an error message and have the original user
entered text in place.

- When I looked at the Struts code, I understand that when converting this property value to an Integer, if it hits a NumberFormatException, Struts
replaces the value with "0" and calls the validate routine. (in BeanUtils.populate, ConvertUtils.convert). Is this correct ?

- Since the FormBean is populated before the validate is called on it, it will have the value "0" instead of the user entered text. So my validate for
an integer will always succeed. 
- When this form bean gets displayed again, it will display the value "0" instead of the user entered text.

Does this mean that such validations SHOULD be done on the client side by way of JavaScript (using onBlur, onSubmit etc) ?

Any suggestions ? 

Thanks,

=================
Kishore Subramanian
Agile Software
Off : 408 999 7128
http://www.agilesoft.com
=================


Re: Validating that values are of the specified type

Posted by Oleg V Alexeev <go...@penza.net>.
Hello Kishore,

Friday, February 09, 2001, 11:36:05 PM, you wrote:

This is darkplace in Struts for my mind. Default values in this case
is not good way.
I use next approach to support validation of int values. In form I
define property of type String to hold value and in validate method I
try to convert string value to int with support of current locale. Any
exception in this stage will be processed and error will be stored in
ActionErrors.
With such scheme you can hold user's input and make stable conversion
mechanism.

KS> Consider the following scenario :

KS> I have a form element "A" that can accept only integers. 
KS> Corresponding to this property, I have a setA(int i) and getA() methods in my ActionForm.

KS> When the user enters a non-integer value for "A" and submits it, the ActionServlet populates the form and calls the validate method on my form bean
KS> (assuming my form is a validating type).

KS> I want to validate in my ActionForm if the value entered is an Integer and if not i want to populate an error message and have the original user
KS> entered text in place.

KS> - When I looked at the Struts code, I understand that when converting this property value to an Integer, if it hits a NumberFormatException, Struts
KS> replaces the value with "0" and calls the validate routine. (in BeanUtils.populate, ConvertUtils.convert). Is this correct ?

KS> - Since the FormBean is populated before the validate is called on it, it will have the value "0" instead of the user entered text. So my validate for
KS> an integer will always succeed. 
KS> - When this form bean gets displayed again, it will display the value "0" instead of the user entered text.

KS> Does this mean that such validations SHOULD be done on the client side by way of JavaScript (using onBlur, onSubmit etc) ?

KS> Any suggestions ? 

KS> Thanks,

KS> =================
KS> Kishore Subramanian
KS> Agile Software
KS> Off : 408 999 7128
KS> http://www.agilesoft.com
KS> =================



-- 
Best regards,
 Oleg                            mailto:gonza@penza.net