0
嗨,我想請一些幫助。我有一個驗證表單所需字段的函數,其中我傳遞了req。字段放在數組中,所以如果爲空,例如first_name返回錯誤消息:「first_name爲空。」 。問題是我想讓郵件中的字段名稱對用戶看起來更「友好」,沒有駝峯或'_'。我怎樣才能做到這一點?如何正確顯示來自check_required_fields函數的錯誤消息?
p.s.這裏是我的代碼:
$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;
}