Page 2 of 2
Re: Validation class
Posted: Sun Jun 01, 2008 8:50 pm
by allspiritseve
pytrin wrote:Ok, good spot
What you are referring to is indeed outside the scope of the model. I was thinking you were talking about the actual data set validation rules, but for the use-case you described you need form validation rules which are context specific.
The model handles all the rules that pertain to the data it is manipulating - but nothing other than that. If you want to make sure the entered password a user submits is valid before updating his email then you should run that check before invoking the model validation routines. You could do that either in the controller, or add specific validation methods to the model (I like that option better if it has to retrieve data, like the password case you described).
I think that should be done in the controller... which you did, sort of. The knowledge that a password needs to be validated before updating is still held by the controller and not the model, but for reusability I think I'd prefer this:
Code: Select all
if ($userMapper->validPassword ($password)):
$userMapper->updateUser ($user);
else:
$view->setError ('invalid password');
endif;
I think the model should check to see it was handed a valid $user object that can be placed into the database. If a title is required to save a blog post, the validation should happen twice: The controller should check and make sure the user filled out the title form field, and if not, load the view and give an error. The data mapper should check to make sure the controller called $post->setTitle ($title) and that the title wasn't empty-- in other words, if the user supplied a perfectly valid title, but the controller does not add it to the $post, then the data mapper should throw an exception but the user shouldn't be notified about the title-- it wasn't their fault. I think this is the best separation of concerns I can come up with-- the controller makes sure the user input is correct, and the model makes sure the controller input is correct.
Re: Validation class
Posted: Sun Jun 01, 2008 9:04 pm
by Eran
Sounds good.
Show us your final code when it's ready

Re: Validation class
Posted: Sun Jun 01, 2008 9:06 pm
by allspiritseve
pytrin wrote:Sounds good.
Show us your final code when it's ready

Will do-- I really shouldn't be coding right now, school is over in a week and a half and I've got exams and papers I need to be doing
