views:

122

answers:

3

I have only recently started web programming and I am pretty much amazed that although I am using a validation library, I still get 20-30 lines of code for validation alone, not counting error messages and callback functions. I am using the Kohana MVC framework and I was wondering if there was any way I can shorten my validation codes. I tried to think of the following

  1. putting validation codes in my model (which is quite had for a noob like me).
  2. creating a really small library to validate entries(something that hooks to the validation class, thus allowing me to call the library for repetitive procedures like user registration, editing and stuff)

Or are there any better and more efficient ways?

A: 

Here's my strategy for dealing with validation code. I suppose by 'validation library', you mean those which just make sure an email is an email, telephone numbers are numerical, and are not business rules in nature.

The idea is to have each business rule code as a functor - if it is PHP, you can get by just using a string to define the function; for other languages, you may have to use the strategy pattern. Define an interface for the functor (not necessary for PHP) and dump it into an array.

Run through the array which will return success, error and an error-code to a buffer. At the end, examine the error buffer and determine which validation has failed. Use it to customise the view.

Here's an example

$checkUniqueUserName = new CheckUniqueUserName();
$checkEmailNotUsed = new EmailNotUsed();
$validator = array();
$validator[$checkUniqueUserName->name()] = $checkUniqueUserName;
$validator[$checkEmailNotUsed->name()] = $checkEmailNotUsed;

$results = array();

foreach ($validator as $v)
{

  $result[$v->getValidatorName()] = $v->execute($userInfo);
}

class CheckUniqueUserName()
{

   public function execute($userInfo)
   {
       // SQL blah blah blah

      if ($bNameUnique)
        return array ('success' => 1)
      else
        return array ('success' => 0, 'error' => "$name is in used", 'error_code' => 'duplicate_name);

   }

}

At the end, you will have a results array, each filled with a validation process, and you know which has failed, and which has not. This can then be passed to the client-side for further processing, like highlighting the failed fields. The error_code an be used to look up the proper error message and formatting applied to it.

I am not really sure about what you mean by call-backs though.

Extrakun
+2  A: 

I use Zend_Validate with Zend_Forms for validation in which the validation code is in the forms init method. All I have to do is pass an array of validators for each element and than run ..

$form->isValid($data);

...outside of the form to validate the data.

The validation array is easily more than 30 lines because I seperate each array entry with a newline. But i guess you will have that if you defining fine grained validation rules for each element right.

And its really easy to define new Validators in Zend.

edit: i discovered a framework that extends the Zend Framework which allows domain objects to contain its own validation. Its called Xyster framework but I could not get it to work on the first try so I haven't tried after that.

andho
+2  A: 

I would highly recommend working on including the validation in the model. Once you are able to do one, any others you create will be much easier. Plus if you have multiple controllers trying to save that data, you will not need to recode the validation. The Kohana docs contain some examples for integrating the validation library and ORM, you should start there.

Another vote for validation in the model.
Tim Lytle