Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Where to place flex validators?  (Read 7777 times)
polesen
Newbie
*
Posts: 5


View Profile Email
« on: June 26, 2008, 05:48:57 »

We have a flex application with many screens, and need to programmatically run all validators in all screens, before doing a specific server action. Hence, we need to have all validators registered somewhere central, to be able to iterate them and execute validate on each. Only if all answer OK, will we execute the rest of the command. The problem is:

* a flex validator is a view-thing: It is defined in mxml, and heavily bound to a specific input field
* the action, which needs to check all validators before executing logic, shall be in a command, as it is business logic

So, where do we place the validators? We can see at least two solutions:

1) Let mediators "register" their validator instances, by putting them into the model in a list, somewhere

This way the command can easily access them all. Bad thing is, that it seems wrong, to keep references to validators in ones model. Maybe this can be made a bit better, by hiding them through some model interface.

or,

2) Let command retrieve all mediators by name, and execute a validation on each, possibly through an interface, if the particular mediator implements that validator interface.

But is it good to have a command retrieve mediators?

Which one is best? Are there other, better solutions to this problem?
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: June 26, 2008, 06:54:41 »

The validators, as you say, are 'view things. They have no place farther back in the app in commands, proxies OR mediators.

Therefore, the validators should be defined in the component that uses them, not centrally.

Create a ValidationProxy that holds a count of the total validations that must pass, and a count of the ones passed so far. Give it a method to clear the validated count and to increment it.
Trigger the validation by sending a notification that all the mediators holding components to be validated are interested in. Before sending this notification, clear the validated count on the ValidationProxy.

Each responding mediator increments the validation count and when it reaches the total to be validated, the ValidationProxy will send the notification that kicks off the action to happen on a good validation.

Since you have multiple screens to validate, you probable don't want to  halt the works, but instead accumulate all the validation errors in an ArrayCollection in the validationProxy.This should be cleared when the validated count is cleared.

If a mediator has a bad validation result it should take the messages and add them to the ValidationProxy's messages and increment the validated counter.

So when the validated count reaches the total to be validated, the Proxy either kicks off the action with an 'all valid' note, or it sends a 'validation failed' note with the a reference to is validation messages list, to be handled in some other way.
 
-=Cliff>
 
Logged
toni_costello
Newbie
*
Posts: 1


View Profile Email
« Reply #2 on: November 28, 2008, 09:11:45 »

But what if I want to reuse a validator for different fields? For example, in an application with about 100 or more input fields, I don't want to create a validator instance for each field. In this case it would be nice to define a set of validators centrally, which can be used for validating different fields.
Logged
Pages: [1]
Print