1. #1
    Ext GWT Premium Member icfantv's Avatar
    Join Date
    Sep 2011
    Location
    Superior, CO
    Posts
    411
    Answers
    20
    Vote Rating
    21
    icfantv will become famous soon enough icfantv will become famous soon enough

      0  

    Question 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.

  2. #2
    Sencha User rohdef's Avatar
    Join Date
    Mar 2010
    Location
    Aarhus, Denmark
    Posts
    67
    Vote Rating
    3
    rohdef is on a distinguished road

      0  

    Default


    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

  3. #3
    Ext GWT Premium Member icfantv's Avatar
    Join Date
    Sep 2011
    Location
    Superior, CO
    Posts
    411
    Answers
    20
    Vote Rating
    21
    icfantv will become famous soon enough icfantv will become famous soon enough

      0  

    Default


    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...

  4. #4
    Sencha User rohdef's Avatar
    Join Date
    Mar 2010
    Location
    Aarhus, Denmark
    Posts
    67
    Vote Rating
    3
    rohdef is on a distinguished road

      0  

    Default


    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());
        }
    });

  5. #5
    Ext GWT Premium Member icfantv's Avatar
    Join Date
    Sep 2011
    Location
    Superior, CO
    Posts
    411
    Answers
    20
    Vote Rating
    21
    icfantv will become famous soon enough icfantv will become famous soon enough

      0  

    Default


    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

  6. #6
    Sencha User PhiLho's Avatar
    Join Date
    Nov 2011
    Location
    Near Paris, France
    Posts
    140
    Answers
    2
    Vote Rating
    1
    PhiLho is on a distinguished road

      0  

    Default


    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?

Thread Participants: 2

Tags for this Thread