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…
You will need to alter
check_required_fieldsmethod to handle the$required_fieldsarray correctly, like this:Edit: I have just noticed that your loop on
$_POSTwill only work as expected if the fields are set. Try the following: