Accessing $_POST array from objects called on page
<p>I have a page that receives POST data from a form. I also have a form validation class that attempts to access the $_POST array when a FormValidation object is created on the page. However, the FormValidation object does not seem to have access to the $_POST array. Why is this happening, and how can I fix this?</p>

<p>EDIT: Added code :)</p>

<p>This code is from register.php</p>

<pre><code>if($_SERVER['REQUEST_METHOD'] == 'POST')
{
var_dump($_POST);

$errors = validate_form();

if(count($errors) &gt; 0)
{
    display($errors);
}
else
{
    act();
}
}
else
{
display();
}

function validate_form()
{
$validator = new FormValidation('POST');

$validator-&gt;add_field('first_name', 'First Name');
$validator-&gt;add_field('last_name', 'Last Name');
$validator-&gt;add_field('email_address', 'Email Address');
$validator-&gt;add_field('password', 'Password');
$validator-&gt;add_field('password_confirmation', 'Password Confirmation');

$validator-&gt;add_rule('first_name', 'required');
$validator-&gt;add_rule('last_name', 'required');
$validator-&gt;add_rule('email_address', 'required');
$validator-&gt;add_rule('email_address', 'is_valid_scarsdaleschools_email');
$validator-&gt;add_rule('password', 'required');
$validator-&gt;add_rule('password_confirmation', 'required');
$validator-&gt;add_rule('password_confirmation', 'matches', array('password', 'Password'));


return $validator-&gt;validate();
}
</code></pre>

<p>The form validation code looks like this</p>

<pre><code>class FormValidation
{
var $fields;
var $rules; //Associative array that maps field_name to array of rules
var $errors;

var $form_method_type;

function FormValidation($form_method_type)
{
    $this-&gt;form_method_type = strtoupper($form_method_type);

    $this-&gt;fields = array(); //maps fields to field labels
    $this-&gt;rules = array();
    $this-&gt;errors = array();
}

function validate() 
{   
    foreach(array_keys($this-&gt;fields) as $field_name)
    {
        if(!array_key_exists($field_name, $this-&gt;rules))
        {
            continue;
        }
        foreach(array_keys($this-&gt;rules[$field_name]) as $rule_name)
        {
            call_user_func_array(array($this, $rule_name), $this-&gt;rules[$field_name][$rule_name]);
        }
    }

    return $this-&gt;errors;
}

function add_field($field_name, $field_label)
{
    $this-&gt;fields[$field_name] = $field_label;
}

function add_rule($field_name, $rule_name, $parameters=array())
{                                                           
    if(!isset($this-&gt;rules[$field_name]))
    {
        $this-&gt;rules[$field_name] = array();
    }

    array_unshift($parameters, $field_name, $this-&gt;fields[$field_name]);
    $this-&gt;rules[$field_name][$rule_name] = $parameters;
}

function var_isset($field_name)
{
    global ${'$_' . $this-&gt;form_method_type};
    var_dump(${'$_' . $this-&gt;form_method_type}[$field_name]);
    return isset(${'$_' . $this-&gt;form_method_type}[$field_name]);
}

function get_value($field_name)
{
    global ${'$_' . $this-&gt;form_method_type};
    return ${'$_' . $this-&gt;form_method_type}[$field_name];
}

    ////////////////////////////////////////////////////////////
/////RULES//////////////////////////////////////////////////
////////////////////////////////////////////////////////////

function required($field_name, $field_label)
{
    if(!$this-&gt;var_isset($field_name))
    {
        $this-&gt;errors[$field_name] = "$field_label is a required field";
    }
}

function is_valid_email($field_name, $field_label)
{
    if($this-&gt;var_isset($field_name))
    {
        if(!validEmail($this-&gt;get_value($field_name)))
        {
            $this-&gt;errors[$field_name] = "$field_label requires a valid email address";
        }
    }
}

function is_alpha($field_name, $field_label)
{
    if($this-&gt;var_isset($field_name))
    {
        if(!ctype_alpha($this-&gt;get_value($field_name)))
        {
            $this-&gt;errors[$field_name] = "$field_label requires alphabetical characters only";
        }
    }
}

function is_alphanumeric($field_name, $field_label)
{
    if($this-&gt;var_isset($field_name))
    {
        if(!ctype_alnum($this-&gt;get_value($field_name)))
        {
            $this-&gt;errors[$field_name] = "$field_label requires alphanumeric characters only";
        }
    }
}

function is_integer_number($field_name, $field_label)
{
    if($this-&gt;var_isset($field_name))
    {
        if(!is_int($this-&gt;get_value($field_name)) || preg_match('[-+]?[0-9]+', $this-&gt;get_value($field_name)) == 0)
        {
            $this-&gt;errors[$field_name] = "$field_label must be an integer";
        }
    }
}

function is_float_number($field_name, $field_label)
{
    if($this-&gt;var_isset($field_name))
    {
        if(!is_float($this-&gt;get_value($field_name)) || preg_match('[-+]?[0-9]*\.?[0-9]+', $this-&gt;get_value($field_name)) == 0)
        {
            $this-&gt;errors[$field_name] = "$field_label must be a number";
        }
    }
}

function is_valid_scarsdaleschools_email($field_name, $field_label)
{
    if($this-&gt;var_isset($field_name))
    {
        if(!validEmail($this-&gt;get_value($field_name)))
        {
            $this-&gt;errors[$field_name] = "$field_label requires a valid email address";
        }

        $email = $this-&gt;get_value($field_name);

        if(!(endsWith($email, 'scarsdaleschools.org', $case=false) || endsWith($email, 'scarsdaleschools.com', $case=false) || endsWith($email, 'scarsdaleschools.k12.ny.edu', $case=false)))
        {
            $this-&gt;errors[$field_name] = "$field_label requires a Scarsdale Schools email address";
        }
    }
}

function max_length($field_name, $field_label, $max_length)
{
    if($this-&gt;var_isset($field_name))
    {
        if(strlen($this-&gt;get_value($field_name)) &gt; $max_length)
        {
            $this-&gt;errors[$field_name] = "$field_label cannot be longer than $max_length characters";
        }
    }
}

function min_length($field_name, $field_label, $min_length)
{
    if($this-&gt;var_isset($field_name))
    {
        if(strlen($this-&gt;get_value($field_name)) &gt; $min_length)
        {
            $this-&gt;errors[$field_name] = "$field_label must be at least $min_length characters";
        }
    }
}

function matches($field_name, $field_label, $match_field_name, $match_field_label)
{
    if($this-&gt;var_isset($field_name) &amp;&amp; !$this-&gt;var_isset($match_field_name))
    {
        $this-&gt;errors[$field_name] = "$field_label must match $match_field_label";
    }
    elseif(!$this-&gt;var_isset($field_name) &amp;&amp; $this-&gt;var_isset($match_field_name))
    {
        $this-&gt;errors[$field_name] = "$field_label must match $match_field_label";
    }
    if($this-&gt;var_isset($field_name) &amp;&amp; $this-&gt;var_isset($match_field_name))
    {
        if(strcmp($this-&gt;get_value($field_name), $this-&gt;get_value($match_field_name)) != 0)
        {
            $this-&gt;errors[$field_name] = "$field_label must match $match_field_label";
        }
    }
}
}
</code></pre>

<p>Thanks for the help!</p>
