douzi1991 2011-06-30 02:19
浏览 12
已采纳

从页面上调用的对象访问$ _POST数组

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?

EDIT: Added code :)

This code is from register.php

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
var_dump($_POST);

$errors = validate_form();

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

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

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

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


return $validator->validate();
}

The form validation code looks like this

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->form_method_type = strtoupper($form_method_type);

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

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

    return $this->errors;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

        $email = $this->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->errors[$field_name] = "$field_label requires a Scarsdale Schools email address";
        }
    }
}

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

function min_length($field_name, $field_label, $min_length)
{
    if($this->var_isset($field_name))
    {
        if(strlen($this->get_value($field_name)) > $min_length)
        {
            $this->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->var_isset($field_name) && !$this->var_isset($match_field_name))
    {
        $this->errors[$field_name] = "$field_label must match $match_field_label";
    }
    elseif(!$this->var_isset($field_name) && $this->var_isset($match_field_name))
    {
        $this->errors[$field_name] = "$field_label must match $match_field_label";
    }
    if($this->var_isset($field_name) && $this->var_isset($match_field_name))
    {
        if(strcmp($this->get_value($field_name), $this->get_value($match_field_name)) != 0)
        {
            $this->errors[$field_name] = "$field_label must match $match_field_label";
        }
    }
}
}

Thanks for the help!

  • 写回答

3条回答 默认 最新

  • douming4359 2011-06-30 05:18
    关注

    There is a small error in your code. What you were writing was essentially $$_POST. You'll need to remove the extra dollar sign from the variable name (corrected methods below). In addition, you needn't worry about calling global $_POST; as $_POST is a superglobal, or automatic global.

    Update: Retrieving $_POST via ${'_' . $this->form_method_type} does not seem to work. The code I sent you works OUTSIDE of the class, but not inside. I wanted to make sure you understood my findings, and the distinction. So while ${'_' . $formMethodType} works outside of the class, inside you'll have to use something like this:

    const TYPE_POST = 'POST';
    const TYPE_GET = 'GET';
    const TYPE_SESSION = 'SESSION';
    const TYPE_SERVER = 'SERVER';
    const TYPE_REQUEST = 'REQUEST';
    
    public $fields = array();
    public $rules = array();
    public $errors = array();
    
    public function __construct($formMethodType)
    {
        $r = new ReflectionClass($this);
        $constants = $r->getConstants();
    
        $formMethodType = strtoupper($formMethodType);
        if (!array_key_exists('TYPE_' . $formMethodType, $constants)) {
            throw new InvalidArgumentException('Could not find type matching $formMethodType : ' . $formMethodType);
        }
        $this->form_method_type = $formMethodType;
    }
    
    public function var_isset($field_name)
    {
        $values = $this->get_values();
        var_dump($values[$field_name]);
        return isset($values[$field_name]);
    }
    
    public function get_value($field_name)
    {
        $values = $this->get_values();
        return $values[$field_name];
    }
    
    public function get_values()
    {
        switch ($this->form_method_type) {
            case self::TYPE_POST:
                $values = $_POST;
                break;
            case self::TYPE_GET:
                $values = $_GET;
                break;
            case self::TYPE_REQUEST:
                $values = $_REQUEST;
                break;
            case self::TYPE_SERVER:
                $values = $_SERVER;
                break;
            case self::TYPE_SESSION:
                $values = $_SESSION;
                break;
        }
        return $values;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度