我最终使用了受@VolkerK 答案启发的自定义解决方案。我应该更清楚地说明我想要什么。我已经用这些信息更新了我的问题。
我仍在寻找更简单的解决方案。
首先,我制作了一些结果数组:
// errors to display
$errors = array();
// fields to ignore in the validation step
$ignore_fields = array();
// return values after validation
$returnvalues = array();
我首先过滤了必填字段:
function required($string){
return trim($string) == "" ? false : $string;
}
$required_errors = array(
"firstname" => "Je moet je voornaam invullen.",
"lastname" => "Je moet je achternaam invullen.",
"email" => "Je moet je e-mailadres invullen."
);
$required_filter = array();
foreach($required_errors as $requiredfieldname => $error){
$required_filter[$requiredfieldname] = array(
'filter' => FILTER_CALLBACK,
'options' => "required",
'flags' => FILTER_NULL_ON_FAILURE
);
}
$required_filtered = filter_input_array(INPUT_GET | INPUT_POST,
$required_filter);
foreach($required_errors as $required => $error){
if(!isset($required_filtered[$required])){
$errors[$required] = $required_errors[$required];
$returnvalues[$required] = "";
$ignore_fields[$required] = "ignored"; // value doesn't matter
}
}
然后检查空字段并使用标准值加载它们
// ignore the other fields if they are empty;
// the actual form has about 10 values here
$maybeempty = array("from", "to", "phonenumber");
$optional_defaultvalue = array(
"from" => 0,
"to" => 0,
"phonenumber" => "",
);
$optional_filter = array();
foreach($maybeempty as $field){
$required_filter[$requiredfieldname] = array(
'filter' => FILTER_CALLBACK,
'options' => "required",
'flags' => FILTER_NULL_ON_FAILURE
);
}
$optional_filtered = filter_input_array(INPUT_GET | INPUT_POST,
$required_filter);
foreach($maybeempty as $field){
if(!isset($optional_filtered[$field])){
$ignore_fields[$field] = "ignored"; // value doesn't matter
$returnvalue[$field] = $optional_defaultvalue[$field];
}
}
在最后一步中,我将构建一个验证数组、自定义错误消息并忽略忽略字段,以跳过空字段或已经出现错误的字段:
$validity_filter = array(
'email' => array(
'filter' => FILTER_VALIDATE_EMAIL,
'flags' => FILTER_REQUIRE_SCALAR
),
'from' => array(
'filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_REQUIRE_SCALAR,
'options' => array('min_range' => 1964, 'max_range' => 2009)),
'to' => array(
'filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_REQUIRE_SCALAR,
'options' => array('min_range' => 1964, 'max_range' => 2009))
);
// filter all ignored fields
$validity_filter = array_diff_key($validity_filter, $ignore_fields);
$validity_filtered = filter_input_array(INPUT_GET | INPUT_POST,
$required_filter);
foreach($validity_filter as $field => $value){
if($value === false){ // NULL values are checked in a previous step
$errors[$field] = $validity_errors[$field]; // show error
$returnvalues[$field] = $_REQUEST[$field]; // return original value
} else {
$returnvalues[$field] = $value; // return filtered value
}
}
// process possible errors and response values