I'm using Kohana, but I think this question is more general.
I have been doing form validation in the controller, and it has worked well so far. But lately, I've ran into a problem.
I have a comments model, and I send comments from a few different controllers to it. Instead of having a validator in every controller, I placed it in the model.
This is great because
- Only one place to change/add validation rules (DRY)
This sucks because
- I obviously need to return a success or failure to the controller, and Kohana's validation library returns errors as an array. So my return looks like this
ON SUCCESS
array('success' => true);
ON FAIL
array('success' => false, $errors);
I can't help but think this is wrong. It feels wrong.
If I do it in the controller, I can simply do
if ($post->validate()) {
doWhatever();
} else {
$this->template->formErrors = $post->errors('form_errors');
}
Which seems better (to me).
Is there a better way to do this? Should I validate in the controller or method? Am I going crazy?