I am using Zend Framework and Doctrine on a project and was wondering if anyone can suggest a good way to integrate Doctrine's validation with Zend_Form. I'm trying to avoid code duplication.
+1
A:
If you are satisfied with aggregating the errors for your form you can do it in the following way: - Make a Zend_Form without validators - On submit, make a Doctrine object and call isValid() - If not valid, don't store, but show form again with an error
What I would recommend you however, is to either - Write both the validators for your Zend_Form and your model. - Write Validators for your Zend_Form that take a Doctrine Model and field as input and then validate the stuff against your model
I suppose the latter option could be a very generic library, useful for even inclusion into the Zend Framework. It is however tedious to write it in the right way.
Peter Smit
2009-10-26 09:17:14
I've been trying something similar to your isValid() suggestion, only I'm doing it in a custom Zend_Form class that stores an instance of the Doctrine model being validated against: public function isValid($data) { $this->getModel() ->fromArray($this->getValues()); if (!$model->isValid()) { ... }I hadn't thought about creating custom validators, I'll look into doing it that way as well, thanks for the suggestion.
rr
2009-10-26 11:43:22