PureMVC Architects Lounge

Announcements and General Discussion => Architecture => Topic started by: immerzeel on December 23, 2007, 03:51:53



Title: Validate form data, where to put it?
Post by: immerzeel on December 23, 2007, 03:51:53
Hi,

I am making some forms in Flash (not Flex) and of course need to validate the data entered by the user.

I already made a model.helpers.ValidateHelper class to keep the validation code centralized.

But.. my question is where do I validate. Now I do it in the Mediator of the form, sending notifications to my AlertMediator to popup with a message.

I have also seen validation methods put in the VO, which seems a very good place , but I don't know if that doesn't clashes with sending to a  AMF service.

Anyone got a good practice on this?


Title: Re: Validate form data, where to put it?
Post by: Joel Hooks on December 25, 2007, 11:17:18
From my experience adding methods to VO objects does not interfere with AMF calls at all. I am using Django AMF, so your mileage may vary, but it completely ignores any methods and passes only public properties of the object (I haven't tested private properties, but implicit getters and setters work).

In Flex, I validate in my MXML form component. I don't know if this is proper, but it works ;)


Title: Re: Validate form data, where to put it?
Post by: Peter on December 26, 2007, 03:17:55
I simply use a Proxy and a SimpleCommand to do validation. In short:

-register the Proxy - ValidateFormProxy - at startup (I need it all the time...)
-the viewcomponent broadcasts an onSubmit event to its Mediator (passing in the data)
-the Mediator then sends a Notification to validate a form at which point a SimpleCommand is invoked (registered as a command with the ApplicationFacade) to retrieve the ValidateFormProxy and passes in the data to be validated (via the body of the Notification)

When things don't validate - the ValidateFormProxy can send a Notification to the Mediator that needs to take action, passing errors in the body of the Notification.

When things validate - the ValidateFormProxy can send a Notification to save the form data.

The ValidateFormProxy can import an external library with utility classes to do the actual validation.


Title: Re: Validate form data, where to put it?
Post by: Joel Hooks on December 26, 2007, 07:58:06
Sounds like I have some refactoring to do. I like your solution Peter.


Title: Re: Validate form data, where to put it?
Post by: Peter on December 27, 2007, 02:23:02
The good thing here (besides the obvious re-use) is that you can do both client side and server side validation this way. I needed the solution to add new users to the application. While I can check - for instance - an email address client side, I can't check if the provided email address is already in the database.
So, if things validate client side, the ValidateFormProxy sends a Notification that invokes a Command to retrieve a Remote Proxy that will try to save the entry. The Remote Proxy catches any server side errors (XML) and sends a Notification to a Mediator to report the errors.