Slick way to validate before navigate?

Tony Smith's Avatar

Tony Smith

24 Apr, 2011 06:48 PM via web

After revisiting this issue many times and making very little progress, I thought I would ask the community. The set-up is easy: I have a module with multiple views, each view with one or more forms, and a "global" navigation control for Next and Previous. I set up a base class, ValidationContainer, that implements an IValidatable interface defined by one getter "validators" which is an array of, you guessed it, validators.

When the Next button fires a NavigationEvent.NEXT event, I need to be sure all "viable" validators pass before firing the NavigationEvent.NEXT_COMPLETED that updates the view.

My thought is to "register" any ValidatableContainer that is added to the stage but how can I do this without creating a mediator for each instance? My hope is to do this so that if any other dev adds a new ValidatableContainer-form to a given view and includes, the architecture will handle validating on Next/Previous.

Any thoughts? Including, "I think you are doing this like a crazy person and you should consider Dentistry".

  1. Support Staff 2 Posted by Joel Hooks on 25 Apr, 2011 02:48 AM

    Joel Hooks's Avatar

    You could make a "ViewValidationMap" or a "ValidatingViewMediatorMap" of sorts. The MediatorMap is already looking at views as they are added to stage, what if it dispatched an event if it was of a certain type and those views were cached in a model?

    https://github.com/epologee/navigator-as3

    Sort of related, not really, but this navigation lib has a lot of promise.

  2. 3 Posted by Tony Smith on 25 Apr, 2011 03:56 AM

    Tony Smith's Avatar

    Exactly!! I get so excited when people actually understand my questions. I am going to check out the MediatorMap and see what kind of magic I can perform there. I am guessing it would work well for "unregistering" validatable views as well. I think that is a great idea.

    I saw that lib posted on a previous thread but never got a chance to check it out. Sounds like now might be a good time. I played around with Cairngorm 3's navigation lib a while back but I am really over the whole Scottish mountain range metaphor.

    Also, the 4.5 was released and I am pretty sure it has some new form components.

    Thanks man.

  3. 4 Posted by Tony Smith on 25 Apr, 2011 02:52 PM

    Tony Smith's Avatar

    Sorry, somewhat new to RL, does the MediatorMap introspect every view added to the stage regardless of whether or not it is mapped? Where does that take place?

  4. 5 Posted by Tony Smith on 25 Apr, 2011 02:57 PM

    Tony Smith's Avatar

    The other option I was considering was to just "register" the validator from the abstract class but how can a base abstract communicate with the event bus since it has no mediator?

    package com.verical.checkout.view.components
    {

    import com.verical.common.interfaces.IValidatable;
    import spark.components.Group;
    
    public class ValidatableContainer extends Group implements IValidatable
    {
        public function ValidatableContainer()
        {
            super();
        }
    
        public function get validators():Array
        {
            return null;
        }
    }
    

    }

  5. 6 Posted by Tony Smith on 27 Apr, 2011 10:13 PM

    Tony Smith's Avatar

    In case someone is interested, I ended up bubbling an event when the base class, ValidatableContainer, was initialized, FlexEvent.INITIALIZE. At the application level, or my case the module level, I added a capture-only listener for the bubbled event in the module's mediator:

     eventMap.mapListener(view, 
                    ValidationEvent.REGISTER, 
                    validationRegisterHandler,
                    ValidationEvent,
                    true);
    

    I ended up having to put that line in the preRegister override because of a race condition. From there I was back into Robotlegs world and dispatched a context event:

     eventDispatcher.dispatchEvent(new ValidationEvent(ValidationEvent.REGISTER, 
                                                event.validatableItem));
    
  6. Tony Smith closed this discussion on 27 Apr, 2011 10:14 PM.

Comments are currently closed for this discussion. You can start a new one.