3

I wrote a small function to check the required fields of a form, are not empty. The function accepts two arguments, 1st is an array with all values from $_POST superglobal. 2nd is the required fields array which I populate.

Have a look:

public $errors = array();

public function validate_fields($fields_array, $required_fields) 
{
    foreach ($required_fields as $key => $value)
    {
        if (array_key_exists($key, $fields_array)) 
        {
            # If key exists in $fields_array
            # check that the key value inside $fields_array is set & isn't empty
            # if it's empty, populate with an error
            if(empty($fields_array[$key][$value]))
            {
                $this->errors[] = "{$key} is empty but in fields_array";
            }
        }
        else 
        {
            # Key does not exists in $fields_array
            # Did someone temper with my html ?
            $this->errors[] = "{$key} is not in fields_array";
        }
    }
    return (empty($this->errors)) ? true : false;
}     

The issue I'm having seems to be related to "if(empty($fields_array[$key][$value]))" statement. my goal is to check that $fields_array key value is not empty based on $required_fields key. I'm sure the statement I'm using is off. If you see anything that you think can be written better, please let me know, as I am new to php. Appreciate the help.

3 Answers 3

5

I think what you're trying to do is:

if(empty($fields_array[$key])) {
    //this means value does not exist or is FALSE
}

If you also want to check for empty-strings or white-space only, then you need something more than just empty. E.g.

if(empty($fields_array[$key]) || !trim($fields_array[$key]))         {
    //this means key exists but value is null or empty string or whitespace only
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Richard, Thanks! I was unaware that empty is returning false on white spaces. Thanks again. By the way, this is how I populate the required fields array: $required_fields = array("email" => "", "email_confirm" => ""); is there a faster approach to setting an array with just the key instead of defining key + empty value ?
$required_fields = array_fill_keys(array("email", "email_confirm","..."), "");
Thanks, that cleans it up a bit.
4

Do note that the above answers will only work for indexed arrays in >PHP 5.4. If you have an associative array you have to use isset instead of empty:

if(isset($fields_array[$key]) && trim($fields_array[$key]) != '')

See https://www.php.net/manual/en/function.empty.php, example #2

Comments

2

You don't need to select the value as an index just key. Where $fields_array[$key] = $value;

if(empty($fields_array[$key]) && trim($fields_array[$key]) != '')

4 Comments

I see you added a few more conditions then Richard, wouldn't using empty and !trim cover everything I want to avoid? (any empty string or white spaces)
Just be careful with empty(), it treats things like 0 or "0" as 'empty', which may not be what you want in some cases. See the comments in php.net/manual/en/function.empty.php for more details. It's probably better to explicitly check for the conditions that you want.
I can't think of any reason I would expect users to input 0, "0", or even "-0" in a form. Even though I have 2 radio buttons for gender during register, I'll assign them to 1 & 2 in the db. If my logic is wrong, correct me :)
I would assign them to male/female or m/f. Its easily readable even when looking at the db without any context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.