Okay, so this is what I have so far. My question now is if my logic and the way I set up the objects is good? This isn't really critique, because I don't know if what I have is right. The only thing I can think of is that I'm expecting the form to be posted via $_POST.
Is there anything I should be doing differently?
Code: Select all
<?php
require_once 'class/formvalidator/rule/length.php';
require_once 'class/formvalidator/rule/required.php';
require_once 'class/formvalidator/rule/regex.php';
/**
* Form validation base class
*/
class formValidator
{
//container for rules
protected $_rules = array();
/**
* Method to add rules
*/
public function addRule(formValidator $rule)
{
$this->_rules = array_merge_recursive($this->_rules, $rule->_add());
}
public function validate()
{
if ($this->_validateRequired() && $this->_validateLength() && $this->_validateRegex())
{
return true;
}
return false;
}
private function _validateRequired()
{
//required rules
if (!empty($this->_rules['required']))
{
foreach ($this->_rules['required'] AS $required)
{
if (empty($_POST[$required]) || (trim($_POST[$required]) == ''))
{
return false;
}
}
}
return true;
}
private function _validateLength()
{
//length rules
if (!empty($this->_rules['length']))
{
foreach ($this->_rules['length'] AS $field => $length)
{
$strlen = strlen($_POST[$field]);
if (($strlen < $length['minLength']))
{
return false;
}
if ($length['maxLength'] && ($strlen > $length['maxLength']))
{
return false;
}
}
}
return true;
}
private function _validateRegex()
{
//regex rules
if (!empty($this->_rules['regex']))
{
foreach ($this->_rules['regex'] AS $field => $pattern)
{
if (!preg_match($pattern, $_POST[$field]))
{
return false;
}
}
}
return true;
}
public function show()
{
echo '<pre>';
print_r($this->_rules);
echo '</pre>';
}
}
Code: Select all
<?php
class formValidator_Rule_Required extends formValidator
{
private $_field;
public function __construct($field)
{
$this->_field = $field;
}
protected function _add()
{
$ret = array();
if (is_array($this->_field))
{
foreach ($this->_field AS $field)
{
$ret['required'][] = $field;
}
} else
{
$ret['required'] = $this->_field;
}
return $ret;
}
}
Code: Select all
<?php
class formValidator_Rule_Length extends formValidator
{
private $_field;
private $_minLength;
private $_maxLength;
public function __construct($field, $minLength=false, $maxLength=false)
{
$this->_field = $field;
$this->_minLength = $minLength;
$this->_maxLength = $maxLength;
}
protected function _add()
{
$ret = array();
$ret['length'] = array(
$this->_field => array(
'minLength' => $this->_minLength,
'maxLength' => $this->_maxLength
)
);
return $ret;
}
}
Code: Select all
<?php
class formValidator_Rule_Regex extends formValidator
{
private $_field;
private $_regex;
public function __construct($field, $regex)
{
$this->_field = $field;
$this->_regex = $regex;
}
protected function _add()
{
$ret = array();
$ret['regex'] = array($this->_field => $this->_regex);
return $ret;
}
}
And I'm calling it like this:
Code: Select all
//form validation object
require_once 'class/formvalidator/formvalidator.php';
$fv = new formValidator();
//add rules
$fv->addRule(new formValidator_Rule_Required(array('username', 'password', 'passwordConfirm', 'email', 'emailConfirm')));
$fv->addRule(new formValidator_Rule_Length('username', 3, 25));
$fv->addRule(new formValidator_Rule_Length('password', 6));
$fv->addRule(new formValidator_Rule_Regex('username', '/[\w]+/'));
var_dump($fv->validate());
$fv->show();
Basically I'm second-guessing everything I'm doing. =/ I think it's because whenever I bring up an OO subject, there's so many different ways of doing things that I lose interest halfway through the topic because I'm completely lost. I'm learning slowly but surely.
So far the validator is working good. I should implement some tests (note to maugrim

).