-
30 Jan 2012 10:29 AM #1
Unanswered: How do I setup a composite validator?
Unanswered: How do I setup a composite validator?
I am creating a form that has a checkbox next to a label/text field. When the checkbox is checked, the text field needs to be required (not empty, valid email address, etc...).
Do I really need to write my own Validator class that extends AbstractValidator to accomplish this? This seems like a common enough use case that maybe this already exists but I'm missing it.
Thanks.
-
30 Jan 2012 11:37 AM #2
I haven't seen one, and I don't think there is, but creating such a validator seems like an easy enough task. Actually I would most likely listen for the checkbox change state and then use textField.setAllowBlank to the state of the checkbox
-
30 Jan 2012 11:41 AM #3
Yea, that's what I was thinking. I'm just not sure I want to pass in UI components into my Validator which would then effectively tightly couple my UI to my Validator - which is a stateless (and UI-less) object.
But then, it is a highly customized feature...
-
30 Jan 2012 11:46 AM #4
I was thinking more like, skip the validator and do it like this

Code:final CheckBox cb = new CheckBox(); final TextField tf = new TextField(); cb.addChangeHandler(new ChangeHandler() { @Override public void onChange(ChangeEvent event) { tf.setAllowBlank(cb.getValue()); } });
-
30 Jan 2012 11:47 AM #5
Oh yea. MUCH cleaner/simpler... Thanks!
UPDATE: for anyone looking at this, the value of setAllowBlank(...) needs to be the negation in this case because the checkbox being selected indicates that the field is required, which means the parameter value should be false.Last edited by icfantv; 30 Jan 2012 at 1:32 PM. Reason: clarified answer
-
1 Feb 2012 2:24 AM #6
Looks like GWT-Pectin is a good fit for such use case, although it would be an overkill to use it just for such case. But in general, forms need lot of such validations, so it can make sense to add this library to the mix.
I haven't tried to use it yet. Does somebody tried to use it with GXT 3?


Reply With Quote