Luis Majano Forums Notification: Post to Form Validation

Title: RE: Form Validation
Thread: Form Validation
Forum: Need Help?
Conference: ColdBox
User: lmajano

Hi tszao,

There are so many ways to do validation and doing handler
validation is one of them. In a more enterprise application in which I have
domain model objects I would create my objects have their own validate()
methods. I would then use the beanfactory plugin to populate them with data
from the request collection and then validate.

To give you an example, let
say you have a cfc that models an address.

address
city
state
zip
postalcode

Then your cfc would have the getters and setters for this
properties. You can take a look at Brian Rinaldis cfc generator to generate your
objects.

Then you would create a validate() method in your address cfc that
validates the properties and returns a boolean.

[code]
//Inside the
handler, in a form submit
oAddress =
CreateObject("component","model.address");
//populate address object with form
getPlugin("beanfactory").populatebean(oAddress);
//Validate the bean
if (
oAddress.validate() ){
  //send or save
  oAddress.save();
}
else{
  //get
errors
  errors = oAddress.getErrors();
  //redirect to error page.
setNextEvent("...");
}
[/code]

There is more to it, you can create an error
string property in your address cfc to hold errors and then you can retrieve
them for display. This method is called form bean validations.

As you can
see, validations can be done in several ways, I see no mistakes in validating
directly in the handler or withing your domain beans. It all depends on your
design and how OO your application is. Hope this helps?

http://www.luismajano.com/forums/index.cfm?event=ehMessages.dspMessages&threadid=099E0187-123F-6116-421FA3B9B2F4006A