Page 1 of 1

How To Handle Validation in Model using PHP?

Posted: Wed Jan 14, 2009 3:36 pm
by gabriel1836
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).

Re: How To Handle Validation in Model using PHP?

Posted: Wed Jan 14, 2009 5:28 pm
by Theory?
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:

Code: Select all

Validator::isValid('email', $email);
And that would use the emailRule object to validate the $email variable.

Right?

Re: How To Handle Validation in Model using PHP?

Posted: Wed Jan 14, 2009 5:43 pm
by Eran
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

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...
     }
}
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/

Re: How To Handle Validation in Model using PHP?

Posted: Wed Jan 14, 2009 5:47 pm
by gabriel1836
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:

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' 
    )
);
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?

Re: How To Handle Validation in Model using PHP?

Posted: Wed Jan 14, 2009 6:32 pm
by Eran
Did you see my post? I think you were writing while I posted it.

Re: How To Handle Validation in Model using PHP?

Posted: Thu Jan 15, 2009 2:01 pm
by gabriel1836
@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.

Re: How To Handle Validation in Model using PHP?

Posted: Fri Jan 16, 2009 7:24 am
by Jenk
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?

Posted: Fri Jan 16, 2009 7:31 am
by Eran
gabriel1836 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?
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 so