I have been trying to make a custom validation class, but it is bothering me that it only is test thing last element it sees. I need it to test everything I put into it.
<?php
class Valid {
function validate(array $args){
$errors = NULL;
//********************************
//Find each key validation.
//********************************
foreach ($args as $key => $value){
//*********************************
//Check for letters only, a-z, A-Z.
//*********************************
if ($key == 'letters'){
$letters = preg_match('/^\pL+$/u', $value) ? TRUE : FALSE;
$letters_value = $value;
$letters_array[] = $letters;
}
//*********************************
//Check for numbers only, 0-9.
//*********************************
if ($key == 'numbers'){
$numbers = preg_match('/^[0-9]+$/', $value) ? TRUE : FALSE;
$numbers_value = $value;
$numbers_array[] = $numbers;
}
//*********************************
//Check for vaild email address.
//*********************************
if ($key == 'email'){
$email = filter_var($value, FILTER_VALIDATE_EMAIL) ? TRUE : FALSE;
$email_value = $value;
$email_array[] = $email;
}
//*********************************
//Check for empty string.
//*********************************
if ($key == 'empty'){
$empty_it = (trim($value) == FALSE) ? TRUE : FALSE;
$empty_array[] = $empty_it;
}
}
//********************************
//Check if you failed letters only.
//********************************
if ( count($letters_array) != count(array_filter($letters_array)) ) $errors .= 'You can only enter letters a-z or A-Z.<br />';
//********************************
//Check if you failed numbers only.
//********************************
if ( count($numbers_array) != count(array_filter($numbers_array)) ) $errors .= 'You can only enter numbers 0-9.<br />';
//*************************************
//Check if you failed email validation.
//*************************************
if ( count($email_array) != count(array_filter($email_array)) ) $errors .= 'You must enter a vaild e-mail address.<br />';
//*************************************
//Check if you empty string.
//*************************************
if ( count($empty_array) != count(array_filter($empty_array)) ) $errors .= 'You must enter a value for each required field.<br />';
//********************
//Display the errors.
//********************
if ($errors) echo '<h3>Oops..</h3>'. $errors; var_dump($args); die;
}
}
And then I call it like this:
$Validate->validate( array('empty' => $display_name, 'empty' => $password, 'empty' => $password_again,
'empty' => $gender, 'empty' => $month, 'empty' => $day, 'empty' => $year, 'email' => $email, 'letters' => $gender,
'numbers' => $month, 'numbers' => $day, 'numbers' => $year) );
But my result is this:
array(4) { ["empty"]=> string(4) "2013" ["email"]=> string(0) "" ["letters"]=> string(0) "" ["numbers"]=> string(4) "2013" }
Any help?
You cannot reuse keys in arrays. The last key - value pair will overwrite all the previous.
Write an array like:
$Validate->validate(array(
'empty' => array($display_name, $password, $password_again, $gender, $month, $day, $year, $email),
'letters' => array($gender, $month),
'numbers' => array($day, $year).
));
and parse it appropriately:
if (isset($args["key"])) {
foreach ($args["key"] as $value) {
// ...
}
}
// and this for every method to check
array('empty' => $display_name, 'empty' => $password, 'empty' => $password_again,
'empty' => $gender, 'empty' => $month, 'empty' => $day, 'empty' => $year, 'email' => $email, 'letters' => $gender,
'numbers' => $month, 'numbers' => $day, 'numbers' => $year)
This is not a valid hash. Keys must be unique.
You could, however, pass something like this:
array(
array('empty' => $display_name),
array('empty' => $password),
array('empty' => $password_again),
array('empty' => $gender), ...
)
and modify your loop appropriately.
Documentation: http://php.net/manual/en/language.types.array.php
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.