2009-12-05 41 views
0

我想通过将发布的值放入变量和这些变量到数组中,然后循环遍历它们并输出错误消息填写。 我有两个问题。首先,即使字段为空或者==为'未定义',if语句也会运行所有值,其次我不知道如何输出变量的实际名称而不是变量值。例如PHP数组的表单验证 - 打印一个变量的名称

$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']); 

感谢

回答

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 />"; 
    } 
} 
+0

就个人而言,我会使用ctype_digit或类似的东西,而不是is_numeric,因为is_numeric接受科学记数法,十六进制等等(这不是大多数人在验证表单时要查找的东西)。 – preinheimer 2009-12-06 00:32:48

1

至于你的第一个代码块去你的IF语句中有一个错误。您正在设置$ v ='undefined'这将每一次评估为真。您需要为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 
     } 
    } 
+0

我无法看到它们,它们是什么? – andrew 2009-12-05 23:46:28

+0

Oh im missing the == – andrew 2009-12-05 23:47:08

+0

@andrew - 编辑并解释 – 2009-12-05 23:47:12