I currently implementing a website, using PHP and my own MVC framework.
When user execute operation like add/edit I first validate input. If something went wrong the model return a Result object, containing list error messages of what went wrong, to controller.
The controller, in case errors occur, forward the Result object messages to View for rendering
my question: Is it wise for the model to sends a list of text message describing the error, or instead it is better to return a list of error codes(int), and it will be the responsebility of the controller to parse them and convert them to text messages, before passing it to view? Any help would be appreciated
EDIT - Adding some code ----
class Result{
...
function addError($msg);
function getErrors(); // return an array of messages
function isSuccess(); // return bool.
...
}
class MyModel extends Model{
...
function save(){
$result = new Result();
// error occur
$result.addError('bla bla1');
// another error occur
$result.addError('bla bla2');
if(!$result->isSucces()){
return $result;
}
}
....
}
class MyController extends Controller{
...
function actionSave(){
$result = $this->model->save($data2save);
if(!$result->isSuccess()){
// pass $result to View
}
}
....
}
I added some code so it would be easier to understand. I just think that adding the text_message to Model make it more coupling to controller and less generic. By using error_code, one controller could interprate it differently than another controller, so the model would be much generic
Aucun commentaire:
Enregistrer un commentaire