Maybe inspired by the Zend-Framework Validation .
So, define the wizard:
class BaseValidator { protected $msgs = array(); protected $params = array(); abstract function isValid($value); public function __CONSTRUCT($_params) { $this->params = $_params; } public function getMessages() {
And then create your own validators:
class EmailValidator extends BaseValidator { public function isValid($val=null) { // if no value set use the params['value'] if ($val==null) { $val = $this->params['value']; } // validate the value if (strlen($val) < $this->params['maxlength']) { $this->msgs[] = 'Length too short'; } return count($this->msgs) > 0 ? false : true; } }
Finally, your inline array may become something like:
$this->name = new EmailValidator( array( 'maxlength' => 10, 'minlength' => 2, 'required' => true, 'value' => $namefromparameter, ), ), );
verification can be performed as follows:
if ($this->name->isValid()) { echo 'everything fine'; } else { echo 'Error: '.implode('<br/>', $this->name->getMessages()); }
justastefan
source share