aspose file tools*
The moose likes Struts and the fly likes Question about the required validator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Question about the required validator" Watch "Question about the required validator" New topic
Author

Question about the required validator

Sean Walker
Ranch Hand

Joined: Feb 04, 2004
Posts: 64
In reading 'Struts in Action' (I just realized that the name is a play on words - aren't I slow...), I came across the following sentence in the section describing the 'required' validator:
Since browsers do not submit empty fields, any field that isn�t required will skip
all validations if the field is null or has a length of zero.

I have read this through many times now, but I'm lost. Could someone please explain what Mr Husted is getting at?


Sean Walker<br />PMP, SCEA, SCWCD, SCJP
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
any field that isn�t required will skip all validations if the field is null or has a length of zero
For example, if my rules state that a field is required and must be a valid email format, then if the field is empty, an ActionError will be created.
If my rules state that a field is not required and it must be a valid email format, then if the field is empty, it's not checked against the proper email format, but if the field is not empty, the value entered into it must match a valid email format.
Clear?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Wow, I can't believe I've never caught that sentence. It doesn't make a whole lot of sense for a number of reasons.
For starters, browsers do submit empty text fields as an empty String "". (Note that I'm talking text fields, checkboxes are another story.) In addition, declaring a field as depends="email" or any other built-in validation will still run the required validation by default. Just take a look at validator-rules.xml - they all depend on required. So if you declare a field with depends="email" it will still function as depends="required, email"!


A good workman is known by his tools.
Sean Walker
Ranch Hand

Joined: Feb 04, 2004
Posts: 64
Thank you Dirk, that was quite helpful until I read Marc's post. At least it clarified what the author meant. However, what Marc said seems to be in conflict with this. Specifically, since most of the basic validators imply the required validator, how can a rule using a basic validator not be required?
Also, as Marc points out, I also understood that unchecked checkboxes are the only field type that are not present in form submissions.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5002
    
    8

Not sure which validator-rules.xml is looking at but the one that I have has depends="" for most of the basic validators, email included. Only exception are the range validators. What Dirk said sounds right to me.


Junilu - [How to Ask Questions] [How to Answer Questions]
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Ok, I just downloaded a fresh copy of 1.1 and it's depends="". So, my apologies on that one. I very well could have tweaked mine a while ago and forgot about it.
Still, the "Since browsers do not submit empty fields," part of the sentence could be omitted.
Sean Walker
Ranch Hand

Joined: Feb 04, 2004
Posts: 64
I think they must have dropped the required dependencies on the basic validators in a recent release of the Validator package, because Ted Husted wrote in his book Struts in Action that most of the basic validators are declared dependent on the required validator.
Like most of the other standard validators, the mask validator is declared to be dependent on the required validator. Therefore, if a field depends on both required and mask, then the required validator must complete successfully before the mask validator is applied.

[ April 29, 2004: Message edited by: Sean Walker ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question about the required validator