-- Drew Bertola <[EMAIL PROTECTED]> wrote
(on Friday, 21 September 2007, 11:52 AM -0700):
> Kexiao Liao wrote:
> > Where is the best place to do the input data(from POST variables) validation
> > in the Zend Framework?
> >
> > We can do the validation in following places:
> > 1. Controller Class file
> > 2. Use Defined Models Class file
> >
> > Which one is the better choice?
>
> Good question. I've been creating a form processing utility class for
> every form, then calling it from the action controller. But
> intuitively, I would have put it in the model.
>
> The reason I didn't is that if I say:
>
> class book extends Zend_Db_Table_Abstract {}
>
> $table = new book();
> $book = $table->find(1);
>
> $errors = $table->validateInput($_POST); // or whatever...
>
> This feels awkward and hard to understand. Also, you tend to lose the
> extra value that the validator object holds.
>
> I suppose you could cobble together a composite model using a table
> model together with validation, filtering, error message preparation,
> etc (the stuff I now do in my form utility classes).
I like to think of models as black boxes: you put stuff in, you get
stuff out. You can certainly do validation in them, but it's easier to
test the validation separately if its contained separately -- so, like
you, I typically put this in a separate class (usually an action
helper).
Regardless of whether the model does validation (perhaps by passing
validation off to another object) or the controller does, you probably
*shouldn't* pull directly from POST or GET in your model, but instead
have data passed in. This makes it possible to re-use your model classes
in other situations (CLI scripts, web services), decoupling them from
the web server.
--
Matthew Weier O'Phinney
PHP Developer | [EMAIL PROTECTED]
Zend - The PHP Company | http://www.zend.com/