kht
kht

Reputation: 317

Cant seem to append to array in php

This simple script should theoretically check the form for errors and then print any errors it finds.

The formValidate function takes in a form array, each field in the form has a value which is validated. The field also has an errors key whose value is an empty array. I am trying to append any errors I find to the errors array of the particular field. I then return the form array when I am done.

I later print out all the errors for the fields. Unfortunately the errors never show up.

I have been breaking my head over this for many hours now and I cant for the life of me figure out why the form errors in my script are not appeding.

Any help would be greatly appreciated!

# get value from array
function array_get(array $array, $key, $default = null)
{
    return (array_key_exists($key, $array)) ? $array[$key] : $default;
}

# get string value from array
function array_get_string(array $array, $key, $default = '', $trim = true)
{
    $val = array_get($array, $key);
    if (is_string($val)) {
        return ($trim) ? trim($val) : $val;
    }
    return $default;
}

function validateForm($form)
{
    // validateField each field
    foreach ($form as $field)
    {
        foreach ($field['validation'] as $validate)
        {
            switch($validate)
            {
                case 'email':
                    if(!filter_var($field['value'], FILTER_VALIDATE_EMAIL)) {
                        $field['errors'][] = $field['value'] . ' is an invalid email address.';
                    }
                break;
                case 'number':
                    if(!preg_match('/^[0-9 ]+$/', $field['value'])) {
                        $field['errors'][] = $field['value'] . ' is an invalid number.';
                    }
                break;
                case 'alpha':
                    if(!preg_match('/^[a-zA-Z ]+$/', $field['value'])) {
                        $field['errors'][] = $field['value'] . ' contains invalid characters. This field only accepts letters and spaces.';
                    }
                break;
            }
        }
    }

    return $form;
}

// $post = filter_input_array( INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS );
$post = $_POST;
$ajax = array_get_string($post, "request_method") == 'ajax';

# select data that needs validation
$form = array(
    'fullname' => array(
        'value' => array_get_string($post, "full-name"),
        'validation' => array('alpha', 'required'),
        'errors' => array(),
    ),
    'emailaddress' => array(
        'value' => array_get_string($post, "email-address"),
        'validation' => array('email'),
        'errors' => array(),
    ),
    'activites' => array(
        'value' => array_get_string($post, "activites"),
        'validation' => array('default'),
        'errors' => array(),
    ),
    'country' => array(
        'value' => array_get_string($post, "country"),
        'validation' => array('default'),
        'errors' => array(),
    ),
    'contactpreference' => array(
        'value' => array_get_string($post, "contact-preference"),
        'validation' => array('default'),
        'errors' => array(),
    ),
    'message' => array(
        'value' => array_get_string($post, "message"),
        'validation' => array('alpha'),
        'errors' => array(),
    ),
);

// validate the form
$form = validateForm($form);

foreach ($form as $field)
{
    foreach ($field['errors'] as $error)
    {
        echo $error . '<br />';
    }
}

Upvotes: 1

Views: 147

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

foreach creates a copy of the array and works on it. If you need to make changes to the original array, pass it by reference like so:

foreach($form as &$field) {
    ...
}

Upvotes: 5

Related Questions