Validation class

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Validation class

Post by allspiritseve »

pytrin wrote:Ok, good spot :P
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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Validation class

Post by Eran »

Sounds good.

Show us your final code when it's ready :P
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Validation class

Post by allspiritseve »

pytrin wrote:Sounds good.

Show us your final code when it's ready :P
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 :banghead:
Post Reply