PHP Classes

data in validation set

Recommend this page to a friend!

      Validate form elements  >  All threads  >  data in validation set  >  (Un) Subscribe thread alerts  
Subject:data in validation set
Summary:why put the data into the validationset manually?
Messages:2
Author:rudie dirkx
Date:2011-01-11 08:57:11
Update:2011-01-11 09:00:19
 

 


  1. data in validation set   Reply   Report abuse  
Picture of rudie dirkx rudie dirkx - 2011-01-11 08:57:11
If you have to 'manually' put the data into the validationset, it's kind of useless isn't it? The validationset and the data should be separate. Something like this:

$v = new Validator;
$v->add('username', new Validate_Unique('Username must be unique!'));
$v->add('password', new Validate_NotEmpty('Please enter a password...'))
$v->add('position', function($v) {
return rand(0, 1) ? 'This is not a valid function!!' : true;
});
etc

or like

$v->add(array(
'username' => new Validate_...,
'password' => new Validate_...,
'position' => function($v) { }
));

and then to validate:

$v->validate($_POST);
or
$v->validate($this->data);
if you use a framework.


PS. I've only seen the example file (by far the most important always), not the actual class.

  2. Re: data in validation set   Reply   Report abuse  
Picture of rudie dirkx rudie dirkx - 2011-01-11 09:00:19 - In reply to message 1 from rudie dirkx
If you use it like that, you can extend the generic validation class to specific forms, eg. NewUserValidator, and put the entire validationset inside the class. Using is than possible in only a few lines:

$v = new NewUserValidator;
$v->validate($_POST);

The validation rules are inside the constructor/init of the NewUserValidator class.