カスタム検証クラスを作成しようとしていますが、それが最後の要素をテストするだけであることが気になります。入れたものすべてをテストするために必要です。
<?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;
}
}
そして、私はそれを次のように呼びます:
$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) );
しかし、私の結果はこれです:
array(4) { ["empty"]=> string(4) "2013" ["email"]=> string(0) "" ["letters"]=> string(0) "" ["numbers"]=> string(4) "2013" }
何か助けはありますか?