你好!
我的项目有点问题。我有一个表单验证php文件,它有一个部分,用于检查表单的字段是否为字段:
function has_presence($value) {
return isset($value) && $value !== "";
}
function validate_presences($required) {
global $errors;
foreach($required as $field) {
$value = $_POST[$field];
if (!is_array($value)) {
$value = trim($value);
}
if (!has_presence($value)) {
$errors[$field] = $field . "\" cannot be blank.";
}
}
}
在表单的验证中,我使用数组的参数调用此函数,如下所示:
$required = array("title", "genres[]", "year", "runtime", "directors[]", "writers[]", "actors[]", "characters", "imdb", "plot", "review", "rating");
validate_presences($required);
问题在于那些数组字段名...在我的表单中我真的必须使用数组,但是这个函数无法处理它们(未定义的索引通知)。
你能帮帮我吗?我可以更改功能还是有其他选择吗?
感谢您帮我解决问题。在这里
编辑:
正如l' L' l建议的那样,我通过这种方式在valitation部分更改了我的代码:
$required = array("title", array("genres"), "year", "runtime", array("directors"), array("writers"), array("actors"), "characters", "imdb", "plot", "review", "rating");
但后来我收到了以下警告:
Warning: Illegal offset type in ...\validations.php on line 52
这是$ value = $ _POST [$ field];我职能的一部分。
答案 0 :(得分:0)
如果$required
中的其他项是数组,则应按如下方式定义:
$required = array("title", array("genres"), "year", "runtime", array("directors"), array("writers"), array("actors"), "characters", "imdb", "plot", "review", "rating");
将$required
中的数组字段设置为数组。