How To Handle Validation in Model using PHP?
Moderator: General Moderators
-
gabriel1836
- Forum Newbie
- Posts: 16
- Joined: Sat Jan 03, 2009 12:45 am
- Location: Logan, UT
How To Handle Validation in Model using PHP?
So I am using Zend_Framework's Zend_Db classes to define the data access layer but I have constructed a custom model layer for my domain objects and each domain object has access to one or more gateways.
My question is how to best handle validation within the model? I've seen examples where every property of the model is defined with specific rules in a configuration file or I could simply specify a _set method for each property that handles each property's validation rules.
P.S. I don't want to use Zend_Form because this feels like tying the view to the model and because I really want my models to remain framework agnostic as much as possible (though I might use Zend_Validator under the covers).
My question is how to best handle validation within the model? I've seen examples where every property of the model is defined with specific rules in a configuration file or I could simply specify a _set method for each property that handles each property's validation rules.
P.S. I don't want to use Zend_Form because this feels like tying the view to the model and because I really want my models to remain framework agnostic as much as possible (though I might use Zend_Validator under the covers).
Re: How To Handle Validation in Model using PHP?
What if you had a static Validator that accepted Rule objects and just ran the isValid() method that accepts two parameters, namely the rule and the content being validated.
So you would call:
And that would use the emailRule object to validate the $email variable.
Right?
So you would call:
Code: Select all
Validator::isValid('email', $email);Right?
Re: How To Handle Validation in Model using PHP?
I integrate Zend_Filter_Input into my ZF models, which is a validation/filter chain. I declare validators and filters per model, and my base model class handles the validation using the Filter_Input class.
Most of my models look like this
I wrote a lengthy blog post on the whole process awhile back (a little outdated). Might be of interest - http://www.techfounder.net/2008/05/21/m ... rk-part-2/
Most of my models look like this
Code: Select all
<?php
class User_Items extends Octabox_Db_Model {
protected $_name = 'items_to_users';
protected $_validators = array(
'item_id' => 'Digits',
'user_id' => 'Digits',
'item_type' => 'Digits',
'rank' => 'Digits',
'content' => 'NotEmpty'
);
protected $_filters = array(
'content' => 'StripTags'
);
}
//Inside some controller
public function newAction() {
if( $this -> isPost() ) {
$userItems = new User_Items();
$result = $userItems -> isValid($_POST);
//Handle results - show user feedback, redirect etc...
}
}-
gabriel1836
- Forum Newbie
- Posts: 16
- Joined: Sat Jan 03, 2009 12:45 am
- Location: Logan, UT
Re: How To Handle Validation in Model using PHP?
I'm thinking that I'll use a Validator class just like you're suggesting (Zend_Framework includes an excellent built-in class for this precise thing) but what I'm wondering more generically is whether I should do something like this:
Or something like this:
And then use a generic validate() function that runs through the configured rules for the model.
Any recommendations one way or the other, or maybe a suggestion that runs in a totally different direction?
Code: Select all
protected function _setEmail($value)
{
if(Validator::isValid('email', $value) {
$this->_row->email = $value
}
}Or something like this:
Code: Select all
protected $_rule = arrray (
'email' => array (
'rule' => 'email'
)
);Any recommendations one way or the other, or maybe a suggestion that runs in a totally different direction?
Re: How To Handle Validation in Model using PHP?
Did you see my post? I think you were writing while I posted it.
-
gabriel1836
- Forum Newbie
- Posts: 16
- Joined: Sat Jan 03, 2009 12:45 am
- Location: Logan, UT
Re: How To Handle Validation in Model using PHP?
@pytrin:
I read your post. It has been very helpful. Thank you.
I was wondering if you're near posting part 3 where you talk about retrieving data for the model from across multiple tables?
It was actually this requirement specifically that made me want to abstract my domain layer from the data access layer so that the business objects wouldn't need to know about how they are structured in the database.
I read your post. It has been very helpful. Thank you.
I was wondering if you're near posting part 3 where you talk about retrieving data for the model from across multiple tables?
It was actually this requirement specifically that made me want to abstract my domain layer from the data access layer so that the business objects wouldn't need to know about how they are structured in the database.
Re: How To Handle Validation in Model using PHP?
I prefer to have simple Command objects for my validation. Example:
Code: Select all
$validator = new EmailValidator("jenk@devnetwork.net");
$validator->isValid();Re: How To Handle Validation in Model using PHP?
Unfurtunately not... I actually have it 80% done but it is pretty outdated. I wanted to come up with a better scheme for cross-table relationships, hopefully in the next month or sogabriel1836 wrote:@pytrin:
I was wondering if you're near posting part 3 where you talk about retrieving data for the model from across multiple tables?