【发布时间】:2012-09-04 14:06:27
【问题描述】:
您好,我需要一些帮助。我有一个功能来验证表单的必填字段,我在其中传递了 req.数组中的字段,因此如果为空,例如 first_name 返回错误消息:“First_name 为空。” .问题是我想让消息中的字段名称看起来对用户更“友好”,没有驼峰或“_”。我怎样才能做到这一点?
附言这是我的代码:
$required_fields = array('first_name', 'last_name', 'email', 'profileInfo', 'message');
$errors = array_merge($errors, check_required_fields($required_fields));
现在输出错误消息如下所示: “first_name 是必需的”或“profileInfo 是必需的”。 函数是这样的:
function check_required_fields($required_fields) {
$field_errors = array();
foreach($_POST as $field=>$value){
if(empty($value) && in_array($field, $required_fields) === true){
$field_errors[] = "the " . $field . " is required.";
//break 1;
}
}
return $field_errors;
}
【问题讨论】:
标签: php function field required