如何从check_required_fields函数正确显示错误消息?

Hi i'd like some help please. i'm having a function for validating required fields of forms, in which i pass the req. fields in an array, so if is empty e.g first_name returns an error message: "The first_name is empty." . The problem is that i would like to make the name of the field in the message to look more "friendly" to the user, no camelCases or '_'. How can i achieve this?

p.s. here's my code:

$required_fields = array('first_name', 'last_name', 'email', 'profileInfo', 'message');
$errors = array_merge($errors, check_required_fields($required_fields));

Right now the output error message looks like : "The first_name is required" or "The profileInfo is required". The function is this:

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;

}

You could give each required field a label...

$required_fields = array(
        'first_name' => 'First Name',
        'last_name' => 'Last name',
        'email' => 'Email Address',
        'profileInfo' => 'Profile information',
        'message' => 'Message'
    );
$errors = array_merge($errors, check_required_fields($required_fields));

You will need to alter check_required_fields method to handle the $required_fields array correctly, like this:

function check_required_fields($required_fields)
{
    $field_errors = array();
    foreach ($_POST as $field => $value)
    {
        if (empty($value) && array_key_exists($field, $required_fields) === true)
        {
            $field_errors[] = "the " . $required_fields[$field] . " is required.";
            //break 1;
        }
    }
    return $field_errors;
}

Edit: I have just noticed that your loop on $_POST will only work as expected if the fields are set. Try the following:

function check_required_fields($required_fields)
{
    $field_errors = array();
    foreach ($required_fields as $field => $label)
    {
        $value = $_POST[$field];

        if (empty($value))
        {
            $field_errors[] = "the " . $label . " is required.";
            //break 1;
        }
    }
    return $field_errors;
}