Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8086493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:28:05+00:00 2026-06-05T18:28:05+00:00

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

  • 0

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 />';
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:28:09+00:00Added an answer on June 5, 2026 at 6:28 pm

    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) {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this simple script: function paginateUsers(page){ get( _config_remote_host+'/users?page='+page,function(json){ json = JSON.parse(json); var _html
After loading a PHP template (using jQuery's load function), this simple script won't make
This is a simple script to select a State then depending on the State
jQuery val() didnt working, this is the simple script: $(#com_form).submit(function () { var name
Hi, I have made this simple script but I need to have it check
I built this simple script to input a certain number, Now I want to
I've got this simple Perl script: #! /usr/bin/perl -w use strict; use Data::Dumper; my
This might be a dumb question but I can't get this super simple script
I have some questions about the performance of this simple python script: import sys,
Suppose I get, on a page, this simple html/script : <a id=hey href=#>try</a> var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.