我正在尝试通过将post值放入变量并将这些变量放入数组然后循环遍历它们并输出未填充的字段的错误消息来进行表单验证。 我有两个问题。首先,即使字段为空或= ='未定义',if语句也会针对所有值运行,其次我不知道如何打印出变量的实际名称而不是变量值。例如
$variable = 'hello';
print_x($variable)//prints 'variable' instead of 'hello'
我尝试了两种方法,如下所示。
$error_message = "The following fields must be filled in:<br />";
$fields_to_validate_arr = array($category,$producer,$product_name,$image_name,$description,$stock_quantity,$min_sale);
foreach($fields_to_validate_arr as $v){
if(empty($v) || $v = 'undefined'){//using variable bariables
//increment the error message with a custom error message.
$error_message .= "->" . ucwords(str_replace('_',' ',$v)) . "<br />";//no need to use variable variables
}
}
我使用变量
的另一种方法$error_message = "The following fields must be filled in:<br />";
$fields_to_validate_arr = array('category','producer','product_name','image_name','description','stock_quantity','min_sale');
foreach($fields_to_validate_arr as $v){
if(empty($$v) || $$v = 'undefined'){//using variable bariables
//increment the error message with a custom error message.
$error_message .= "->" . ucwords(str_replace('_',' ',$v)) . "<br />";//no need to use variable variables
}
}
变量在我的代码中进一步分配,如
$category = myescape_function($_POST['category']);
由于
答案 0 :(得分:1)
就你的第一个代码块而言,你的IF语句中有一个错误。您设置 $ v ='undefined',每次都会评估为true。您需要对IF语句使用相等运算符。
$error_message = "The following fields must be filled in:<br />";
$fields_to_validate_arr = array($category,$producer,$product_name,$image_name,$description,$stock_quantity,$min_sale);
foreach($fields_to_validate_arr as $v){
if(empty($v)){ //using variable variables
//increment the error message with a custom error message.
$error_message .= "->" . ucwords(str_replace('_',' ',$v)) . "<br />";//no need to use variable variables
}
}
答案 1 :(得分:1)
没有必要创建自己的输入变量数组,因为你已经有$ _POST:
$_POST = array_map('myescape_function', $_POST);
foreach($fields_to_validate_arr as $v){
if(empty($_POST[$v]) || $_POST[$v] == 'undefined'){
//increment the error message with a custom error message.
$error_message .= "->" . ucwords(str_replace('_',' ',$v)) . "<br />";
}
}
由于这些值没有存储在单独的变量中,因此打印变量名称而不是它的值的问题就会消失。
如果您想获得真正的想象力,可以添加对自定义验证器的支持:
function inputExists($name, &$source) {
return !empty($source[$name]) && 'undefined' != $source[$name];
}
function inputIsNumeric($name, &$source) {
return inputExists($name, $source) && is_numeric($source[$name]);
}
// checks for U.S. phone numbers only
function inputIsPhone($name, &$source) {
if (inputExists($name, $source)) {
// strip whatever non-numeric
$value = preg_replace('/[-.,() \t]+/', '', $source[$name]);
return preg_match('^(1?[2-9]\d{2})?[2-9]\d{6}$', $value);
}
return False;
}
function inputMatchesRE($name, &$source, $RE) {
return inputExists($name, $source) && preg_match($RE, $source[$name]);
}
function nameAndValidator($name, $validator) {
if (function_exists($validator)) {
return array('name' => $name, 'validator' => $validator, 'data' => '');
} elseif (is_numeric($name)) {
// if index is numeric, assume $validator actually holds the name
return array('name' => $validator, 'validator' => 'inputExists', 'data' => '');
} else {
return array('name' => $name, 'validator' => 'inputMatchesRE', 'data' => $validator);
}
}
$fields_to_validate_arr = array('name', 'street' => '/^\d+ +[a-z ]+(ave|st|wy|way|ln|lp|blvd)$/i', 'age'=> 'inputIsNumeric', 'phone' => 'inputIsPhone');
$_POST = array_map('myescape_function', $_POST);
foreach($fields_to_validate_arr as $name => $validator){
list($name, $validator, $data) = nameAndValidator($name, $validator);
if(! call_user_func($validator, $name, $_POST, $data)){
//increment the error message with a custom error message.
$error_message .= "->" . ucwords(str_replace('_',' ',$v)) . "<br />";
}
}