Reputation: 3165
I am using zend framework and this is the skeleton of my model and controller methods
Model Class methods :
_validateRegisterForm($postData)
{
//validating data using Zend_Filter_Input
//Returns instance of Zend_Filter_Input
}
//Return true/false
protected _isNumberAlreadyExists()
{
// I dnt want to perform above validation using zend_validate_db_recordexists
//since i dnt want to mix my dblogic with in buisness logic
}
register($postData)
{
$input=$this->_validateRegisterForm($postData);
if(!$input->isValid())
{
//What should it return to controller
}
if(!$this->_isNumberAlreadyExists($input->number))
{
//What should it return to controller
}
}
controller class api
$this->_model->register($postData)
I want given below condition to return error in same format to controller class
if(!$input->isValid())
{
//What should it return to controller
}
if(!$this->_isNumberAlreadyExists($input->number))
{
//What should it return to controller
}
I can simply return false from model class but for that i have to save zend_filter_Input instance or error message(if number already exists).Thus in that case i have to create another error handler object which can convert error messages into common format and let controller to fetch those error
To me it does not look like a proper way of doing things in zend framework. Would somoneone like to help me about passing error from model to controller in zend framework or suggest some other method to handle errors in zend framework
Upvotes: 3
Views: 840
Reputation: 316969
Two notes before we start:
To solve your problem you can do
public function register()
{
$this->assertThis($postData);
$this->assertThat($postData);
// do something with $postData
}
The methods assertThis
and assertThat
are your validation methods, just that instead of returning something, they will throw an appropriate Exception. Since register is a Command, it should not return anything at all.
In your controller, you simply catch the Exceptions and convert them into a format usable by your Presentation Layer (which is what C+V are)
public function registerAction()
{
try {
$this->foo->register($this->getRequest()->whatever());
} catch (ThisException $e) {
// handle here
} catch (ThatException $e) {
// handle here
}
}
Another option would be to collect any errors inside the object doing the registration. You'd then have to return of course and provide a method on that object to get the errors. So it adds cruft to your objects.
public function register()
{
if ($something === false) {
$this->errors = 'Something was false';
return false;
}
// do something with $postData
}
Your controller would then do
public function registerAction()
{
if (false === $this->foo->register($this->whatever())) {
$errors = $this->foo->getErrors();
// do something with $errors
}
}
A third option would be to use a Notification pattern
An object that collects together information about errors and other information in the domain layer and communicates it to the presentation.
See http://martinfowler.com/eaaDev/Notification.html for an in-depth explanation.
Upvotes: 4