user9853119
user9853119

Reputation:

Store error messages in one array

I want to create a function that checks each filed, If empty add error messages for that field.

If I have the following form:

<form method="post">
    <input type="text" name="name" placeholder="Name">
    <input type="email" name="email" placeholder="Email">
    <input type="submit" name="submit" value="Submit">
</form>

I tried:

$errors = array();
function empty_check($var, $fieldName){
    if( empty($var) ){
        $error = $fieldName . ' can\'t be empty';
        array_push($errors, $error);
    }
}

if (isset($_POST['form_submit'])) {
    empty_check( $_POST['name'], 'Name' );
    empty_check( $_POST['email'], 'Email' );
    print_r($errors);
}

So if I submit the form and both fields are empty, I should get:

Array(
    [0] => Name can't be empty 
    [1] => Email can't be empty 
)

But I get error saying that the $errors inside empty_check function is not defined.

When I add it to the parameters:

function empty_check($var, $fieldName, $errors){
    ..
}

Then I need to add that parameter for each time I run the function, And I get an empty array:

empty_check( $_POST['name'], 'Name', $errors );
empty_check( $_POST['email'], 'Email', $errors );

That returns:

Array()

I tried return, But I get an error [] can't be used for reading:

function empty_check($var, $fieldName){
    if( empty($var) ){
        $errors[] = $fieldName . ' can\'t be empty';
        return $errors[];
    }
}

How should it be?

Upvotes: 1

Views: 1887

Answers (3)

Mike Q
Mike Q

Reputation: 7327

Personally I would write a SpamChecker() class that looked over bad data and empty data and verified the emails. What you are look for could be handled on the frontend with validation and you should have that anyway.

function check_for_empty_form_values($frm)
{
    $keys = array_keys($frm);
    if (empty($keys)) {
        return array("No data set");
    }
    $errors = array();
    foreach ($keys as $field) {
        if (empty($frm[$field])) {
            $errors[] = " Field : " . $field . " not set";
        }
    }
    return $errors;
}


$_POST["first_name"] = "Mike";
$_POST["last_name"] = "Q";
$_POST["middle"] = "";

$res = check_for_empty_form_values($_POST);

if (count($res) > 0) {
    echo " Errors Found " . count($res) . "\n";
}
var_dump($res);

Example command line output:

Errors Found 1
array(1) {
  [0]=>
  string(23) " Field : middle not set"
}

Upvotes: 2

LuisH
LuisH

Reputation: 33

function empty_check($var, $fieldName,&$errors) {
 if( empty($var) ){
    $error = $fieldName . ' can\'t be empty';
    array_push($errors, $error);
   }
}

Upvotes: 0

Jonathan Lam
Jonathan Lam

Reputation: 17351

Variables from the global scope must be declared with the global before use in a function scope. Reference: Variable scope.

Therefore:

function empty_check() {
  global $errors;

  // ..
}

This is similar to using the extern keyword to declare global variables in a function scope in C. This is different from many newer languages like JavaScript, whose functions can inherit variables from the global scope.

Upvotes: 0

Related Questions