我需要帮助在php的形式验证它的复杂性



我在表单验证中遇到问题,我得到的错误类似于

致命错误:未捕获错误:C:\examplep\htdocs\LSR\signup.php中不支持的操作数类型:22堆栈跟踪:#0{main}在第22行的C:\examplep\tdocs\LSR \signupphp中抛出

function check_empty_fields($required_fields_array){
$form_errors = array();
foreach($required_fields_array as $name_of_field){
if (!isset($_POST[$name_of_field]) || $_POST[$name_of_field] == NULL) {
$form_errors[] = $name_of_field . "Is a REQUIRED FIELD";
}
}

return $form_errors;
}

function check_min_length($fields_to_check_length){
$form_errors = array();
foreach($fields_to_check_length as $name_of_field => $minimum_length_required){
if(strlen(trim($_POST[$name_of_field])) < $minimum_length_required){
$form_errors[] = $name_of_field . "Is too short must be {$minimum_length_required} characters long";

}

}
return $form_errors;
}

function check_email($data){
$form_errors = array();
$key = "email";
if(array_key_exists($key, $data)){
if($_POST[$key] != null){
$key = filter_var($key, FILTER_SANITIZE_EMAIL);
if (filter_var($_POST[$key], FILTER_VALIDATE_EMAIL) === false) {
$form_errors[] = $key . "is not a valid email address";
}
}
} 
return $form_errors;
}

function show_errors($form_errors_array){
$errors .= "<p><ul style='color: red;'>";

foreach($form_errors_array as $the_error){
$errors .="<li>{$the_error}</li>";
} 
$errors .= "</ul></p>";
return $errors;

}

试穿http://sandbox.onlinephpfunctions.com/code/c36fd6eb9b3a8e555364d99dd90cd9281100c884

<?php
function check_empty_fields($required_fields_array)
{
if (!is_array($required_fields_array)) {
return array();
}
$form_errors = array();
foreach ($required_fields_array as $name_of_field) {
if (empty($_POST[$name_of_field])) {
$form_errors[] = "'$name_of_field' is a REQUIRED FIELD";
}
}
return $form_errors;
}
function check_min_length($fields_to_check_length)
{
if (!is_array($fields_to_check_length)) {
return array();
}
$form_errors = array();
foreach ($fields_to_check_length as $name_of_field => $minimum_length_required) {
if (isset($_POST[$name_of_field]) && strlen(trim($_POST[$name_of_field])) < $minimum_length_required) {
$form_errors[] ="'{$name_of_field}' is too short must be '{$minimum_length_required}' characters long";
}
}
return $form_errors;
}
function check_email($fields_to_check_email = array('email'))
{
if (!is_array($fields_to_check_email)) {
return array();
}
$form_errors = array();
foreach ($fields_to_check_email as $name_of_field) {
if (isset($_POST[$name_of_field]) && !filter_var($_POST[$name_of_field], FILTER_VALIDATE_EMAIL)) {
$form_errors[] = "'{$name_of_field}' is not a valid email address";
}
}
return $form_errors;
}
function show_errors($form_errors_array)
{
$errors = '';
$errors .= "<p><ul style='color: red;'>";
foreach ($form_errors_array as $the_error) {
$errors .= "<li>{$the_error}</li>";
}
$errors .= "</ul></p>";
return $errors;
}
// Tests...
$_POST = array(
'name' => 'Test',
'null_name' => null,
'empty_name' => '',
'email' => 'test@mail.com',
'invalid_email' => 'test@@mail.com',
);
$empty_errors = array();
$empty_errors = check_empty_fields(array('name', 'null_name', 'empty_name', 'undefined_name'));
echo 'Empty errors:', show_errors($empty_errors), "n";
$min_length_errors = array();
$min_length_errors = check_min_length(array('name' => 4, 'empty_name' => 4));
echo 'Min length errors:', show_errors($min_length_errors), "n";
$email_errors = array();
$email_errors = check_email(array('email', 'invalid_email'));
echo 'Email errors:', show_errors($email_errors), "n";

相关内容

  • 没有找到相关文章

最新更新