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?
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?
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.
Joined: Feb 04, 2004
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.
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.
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.
Joined: Feb 04, 2004
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 ]