I'm new to OOP....
Here's part of my register.php that has a form for user registration...
Code:
If(Input::exists()) {
$validate = new Validate;
$validation = $validate->check($_POST, array(
'username' => array(
'name' => 'Username', //display name if error occurs
'required' => true,
'min' => 2,
'max' => 20,
'unique' => 'users' //fieldname that must be matched to see if fieldname is unique
),
'password' => array(
'name' => 'First Password',
'required' => true,
'min' => 6,
),
'password_again' => array(
'name' => 'Second Password',
'required' => 'true',
'matches' => 'password'
),
'name' => array(
'name' => 'Name',
'required' => true,
'min' => 2,
'max' => 50
)
));
The form must be validated first, so my validation class is used to use the check() method.
Here's my check method in my validation class:
Code:
public function check($source, $items = array()) {
foreach($items as $item => $rules) {
foreach($rules as $rule => $rule_value) {
$value = trim($source[$item]);
$name = $rules['name'];
$item = escape($item);
if($rule === 'required' && empty($value)) {
$this->addError("{$name} is required.");
} else if(!empty($value)) {
switch($rule) {
case 'min':
if(strlen($value) < $rule_value) {
$this->addError("{$name} must be a minimum of {$rule_value} characters.");
}
break;
case 'max':
if(strlen($value) > $rule_value) {
$this->addError("{$name} must be a maximum of {$rule_value} characters.");
}
break;
case 'matches':
if($value != $source[$rule_value]) {
$this->addError("{$name} must match {$name}.");
}
break;
case 'unique':
$check = $this->_db->get($rule_value, array($item, '=', $value));
if($check->count()){
$this->addError("{$name} already exists.");
}
break;
}
}
}
}
if(empty($this->_errors)) {
$this->_passed = true;
}
return $this;
}
I added $name = $rules['name']; in the foreach part of the method so that when the error message is echoed to the user, it will use the 'name' from the registration page that is assigned for a nicer string. I have an addError() method that does this...
Things like this are echoed nicely: Username is required. Name must be a minimum of 2 characters.
It was working just great until I use a 'match' check validation.
In my 'match' switch statement:
Code:
case 'matches':
if($value != $source[$rule_value]) {
$this->addError("{$name} must match {$name}.");
}
break;
.. I cannot do: $this->addError("{$name} must match {$name}.");
When it's checking password_again against password it displays this error message:
"Second Password must match Second Password".
In actuality it SHOULD say, "Second Password should match First Password".
I hope I explained this well... can anyone offer advice how to get this to work better?