views:

232

answers:

1

I am using the Zend MVC framework along with an ORM layer generated with Propel, and I'm trying to figure out the best way to catch exceptions from a Propel object's save() function, and throw them to the Zend Form as errors.

Not all of the exceptions that come out of the Propel object have a way to identify which field caused the error, so I'm wondering if there is a way to add generic error messages to the entire form, rather than being forced to attach each error message to a particular form element.

For example, I have a save() call wrapped in a try/catch block, and I can add the exception->getMessage() to a form element's errors:

try {
    $obj->save();   
    echo 'object saved successfully';
} catch (Exception $e) {
    $form->name->addErrorMessage($e->getCode()." - ".$e->getMessage());
    $form->name->markAsError();
    $form->populate($formData);
}

but I would like to be able to do something like this:

try {
    $obj->save();   
    echo 'object saved successfully';
} catch (Exception $e) {
    $form->addErrorMessage($e->getCode()." - ".$e->getMessage());
    $form->markAsError();
    $form->populate($formData);
}

I hope that makes sense, thanks for the help,

Dave

+1  A: 

Are you thinking about the errors from the database, or from the Propel validation layer (which isn't developed that much, and not used by default in the save() step)?

If you want to use the database errors, keep in mind that they will only return the first error (so the user has to submit four times if they entered three errors). Also, getting the field name out of the error message can be hard. Keep in mind that some keys cover multiple fields ("the combination of name and first_name must be unique").

This is why for example Symfony adds validation in the form layer. There, you can check all fields at once, and return multiple errors. But maybe you already do this, and only want this as a final check?

Jan Fabry
I can check all fields in the form layer with Zend, but I do still want to bubble up Propel errors as a final check. I would ideally incorporate every possible error that could come out of Propel back into the form layer in Zend, but for now I just want to prevent a form submission from going to a nasty PHP error page. I will have to do some research to figure out the best way to unique key constraint errors in Zend_Form. Thanks for your help,
Dave Morris