我的表单以下列方式验证:
//Clear all variables
$formCheck = '';
$rep = '';
$name = '';
$department = '';
$location = '';
$email = '';
$phone = '';
$type = '';
$drink = '';
$notes = '';
$lastVisited = '';
$nextVisit = '';
$clean_formCheck = '';
$clean_rep = '';
$clean_name = '';
$clean_department = '';
$clean_location = '';
$clean_email = '';
$clean_phone = '';
$clean_type = '';
$clean_drink = '';
$clean_notes = '';
$clean_lastVisited = '';
$clean_nextVisit = '';
function validateRep($rep){
...some code...
}
$formCheck = $_POST["formCheck"];
$rep = $_POST["rep"];
$name = $_POST["name"];
$department = $_POST["department"];
$location = $_POST["location"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$type = $_POST["type"];
$drink = $_POST["drink"];
$notes = $_POST["notes"];
$lastVisited = $_POST["lastVisited"];
$nextVisit = $_POST["nextVisit"];
if (validateRep($rep)){
$clean_rep = $rep;
}else{
echo "Invalid Rep";
exit();
}
//.....and so on......
我想知道使用数组而不是单个变量验证是否更高效/更清晰?如果是这样,我将如何解决这个问题,以及如何将不同的验证函数编写在一起(例如,现在我有一个单独的函数来验证每个字段),是否可以通过数组循环?我正在试验,到目前为止这就是我所拥有的:
$unclean['formCheck'] = $_POST["formCheck"];
$unclean['rep'] = $_POST["rep"];
$unclean['name'] = $_POST["name"];
$unclean['department'] = $_POST["department"];
$unclean['location'] = $_POST["location"];
$unclean['email'] = $_POST["email"];
$unclean['phone'] = $_POST["phone"];
$unclean['type'] = $_POST["type"];
$unclean['drink'] = $_POST["drink"];
$unclean['notes'] = $_POST["notes"];
$unclean['lastVisited'] = $_POST["lastVisited"];
$unclean['nextVisit'] = $_POST["nextVisit"];
$clean = array(
'rep', 'name', 'department', 'location', 'email', 'phone', 'type', 'drink', 'lastVisited', 'nextVisit',
);
但我不确定如何从这里开始。
答案 0 :(得分:1)
$rep = filter_input(INPUT_POST, "rep", FILTER_SANITIZE_STRING);
$name = filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING);
$department = filter_input(INPUT_POST, "department", FILTER_SANITIZE_STRING);
$location = filter_input(INPUT_POST, "location", FILTER_SANITIZE_STRING);
if (filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL))
{
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
}
就像那样。
答案 1 :(得分:1)
我会沿着这些方向使用某些东西...只是快速编码,基本上你创建匹配post字段的验证函数,如果验证通过则返回true或false。例如validate_department,validate_type,validate_drink等。如果您的帖子数据中没有任何奇怪的字符(到目前为止它没有),它将起作用
$post_fields = array('rep',
'name',
'department',
'location',
'email',
'phone',
'type',
'drink',
'lastVisited',
'nextVisit'
);
$validate = new Validate();
foreach ($post_fields as $post_var)
{
if (isset($_POST[$post_var]))
{
$validate->validate_data($post_var, $_POST[$post_var]);
}
}
if ($validate->all_fields_valid() === true)
{
echo 'congrats, all validation passed!';
}
else
{
echo 'oh no! error in validation process. please see below errors: <p>' .
$validate->get_error_msg() . '</p>';
}
验证类...如果遇到任何问题,请使用$ errorMsg查看错误消息
class Validate
{
var $valid = 0,
$error = 0,
$errorMsg = '';
function validate_data($var, $data)
{
if (method_exists($this, 'validate_'.$var))
{
if (call_user_func(array($this, 'validate_'.$var), $data) === true)
{
$this->valid++;
}
else
{
$this->throwError('validation for: "'.$var.'" was not considered valid');
}
}
else
{
$this->throwError('validation function for: "'.$var.'" does not exist');
}
}
function throwError($msg = '')
{
if ($msg) $this->errorMsg .= $msg . '<br/>';
$this->error++;
}
function all_fields_valid()
{
if (!$this->error) return true;
return false;
}
/***********************************************
*************************************************
Custom validation functions go below here
Function format: validate_<postFieldName>
Returns: true or false if the data passed is valid or not
*************************************************
*************************************************/
function validate_type($type)
{
if (is_numeric($type)) return true;
return false;
}
function validate_lastVisited($data)
{
}
//etc...............
}