Hi Dennis

This case is best validated by creating a temporary String property in the
actionBean and a @ValidationMethod where you can check whether it is null
(that would be OK) - if it is not null it has to be a number, so you can try
parsing it to a number. If that works the value can be set on the number
property. Else you can add an error to the input ValidationErrors.

public class Whatever extends ActionBean{

String temp;

public String getTemp(){
    return ""+theRealProperty;
}

Integer theRealProperty; // must be null or a number

@ValidationMethod
public void test(ValidationErrors errors){
   if (temp == null) return;
   else{
      try{
         theRealProperty = Integer.parse(temp);
      } catch(Exception e){
         errors.add(new LocalizationError("not.a.number");
      }

}




2009/3/30 Ben Gunter <[email protected]>

> The @Wizard annotation might work for you. With wizard beans, validation
> only applies to fields that were actually rendered in the form that is
> submitted. This normally serves to allow for the inputs to be presented
> across several pages, but it might also work for your case.
>
> If that doesn't work, then you can implement ValidationErrorHandler and
> remove the validation error for the field in question when it is not
> required.
>
> -Ben
>
>
> On Mon, Mar 30, 2009 at 9:53 AM, <[email protected]> wrote:
>
>>
>> Hi,
>>
>> I'm looking for the best way to validate a form field that may or may not
>> be shown in the form.
>>
>> This means that if the field is shown a number is required, if it is not
>> nothing is required. How do I express that - "must be null or a valid
>> number"?
>>
>> Thanks,
>>
>> -dennis
>
>
>
>
> ------------------------------------------------------------------------------
>
> _______________________________________________
> Stripes-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/stripes-users
>
>


-- 
 Morten Matras
 Consultant
 Blob Communication ApS
 Svendsagervej 42
 DK-5240 Odense NĂ˜
 P: (+45) 76 6-5-4-3-2-1
 W: www.blobcom.com
 E: [email protected]
------------------------------------------------------------------------------
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users

Reply via email to