Re: Form Validation

"Diez B. Roggisch" <[email protected]>
Newsgroups gmane.comp.java.enhydra.barracuda.general
Message-ID <[email protected]>
Hi Ming (or Peng? I'm not sure which name is the first name in chinese/asiatic 
names),

>   You can see the code in "LoginScreen.java" of the DiscRack sample.If I
> click the "Submit" without filling the "username" field which has the
> NotNullValidator,will the "LoginValidator" execute?If "yes",will I get the
> Exceptione for the "null" problem?If I comment out the code---
> if (user != null && password != null)---It seems a NullPointException will
> be thrown.But this special code is for the validation for "Null input" and
> a validator for forbiding "blank input" has been defined.Is that redundant?

There are two types of validators: FormElementValidators, which are attached 
to a single form element, and FormValidators, which are attached to the whole 
form.

From the javadoc:

-------
public FormMap validate(boolean deferExceptions)
                 throws ValidationException
 
Validate the entire form (both form level and elements). We start by invoking 
form validators which apply to individual form elements, then we invoke any 
which apply to the entire form
------

So you are right: The LoginValidator is called after all the 
FormElementValidators have been called, even if one of them fails.

But this is not redundant (as long as you're code in the validators isn't 
redundant): Lets assume a Form for registering a new user. It contains three 
elements: NAME, PWD, PWD_CONFIRM.

Now the user must specify an entry for all three of them, so there's a 
NotNullValidator on all of them. Also PWD=PWD_CONFIRM must hold, so you 
define a custom FormValidator that ensures that.

Now if the user enters different PWD and PWD_CONFIRM and _no_ NAME, the 
NotNullValidator of NAME will throw an exception, and the FormValidator will 
complain about not-mathching passwords. So you can supply your user with all 
neccessary informations at once. 

I admit that this example is a little bit misleading, as password-fields 
aren't subject to repopulation. But think of an arrival and departure-date 
whe arrival < departure has to hold. 

However, if you only want to allow for form-wide validation when all the 
element-wise validation has succeeded, you can always call 

try{
 validateElements(true);
 validateForm(true);
} catch(ValidationException ex) {
...
}

Regards,

Diez
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.