处理类中的表单,php

时间:2012-10-05 12:30:06

标签: php forms oop class post

我有一个简单的问题。我一直在开发和设计OO站点,但到目前为止,整个过程的一部分不是OOP。这是处理表单的脚本。

我已经四处寻找并且没有找到答案,所以我不确定是否有可能,希望你们能够为我澄清。

我的表单发布到标准脚本,例如login.php,我希望我的表单发布到类,或者更确切地说,是该类中的方法。这有可能吗?

非常感谢, 杰

2 个答案:

答案 0 :(得分:4)

您不能直接发布到类,但可以将参数发送到该类中的函数,例如:

$class = new Forms();
$class->processForm($_POST, 'login');

// it would call `processForm()` from `Forms` class and send `$_POST` and `login` as parameters 

或者您可以使用__construct()功能:

class Forms {

    function __construct($array, $type){
        switch($type){
            case 'login':
                $this->processLogin($array);
                break;
            //......
        }
    }

    function processLogin($array){
        // do something with $array;
    }

}

$class = new Forms($_POST, 'login');

答案 1 :(得分:0)

您可以使用类似于在构造函数阶段自动处理自身的基类,并使用指定字段继承特定表单。不要忘记一些渲染代码。 (这里没有显示,因为它是特定于系统的。)

class Form {

    protected $name = "generic_form";
    protected $method = "POST";
    protected $url = "";

    protected $fields = array();
    protected $data = array();
    protected $submits = array();
    protected $errors = array();
    protected $required = null;
    protected $submitsrequired = array();


    public function __construct() {
        $this->FillSubmits();
        $this->FillData(); 
        foreach($this->fields as $key => $value) {
            if(isset($this->data[$value["name"]])) {
                $this->fields[$key]["value"] = $this->data[$value["name"]];
            }
        }
        $action = null;
        foreach($this->submits as $key => $value) {
            if (isset($this->data[$value["name"]])) {
                $action = $value["name"];
                break;
            }

        }
        if ($action === null) {
            return;
        }
        if (!$this->innerValidate($action)) {
            return;
        }
        $this->Commit($action);
    }

    protected function Validate($action, $name, $value) {
      // Put validation code here
    }

    protected function Commit($action) {
      // Put code to execute when form is correctly filled here
    }

    protected function FillSubmits() {
        foreach($this->fields as $field) {
            if($field["type"] == "submit")
                $this->submits[] = $field;
        }

    }

    protected function FillData() {
        foreach($this->fields as $field) {
            if(isset($_POST[$field["transmitname"]]))
                $this->data[$field["name"]] = $_POST[$field["transmitname"]];
        }
    }

    protected function innerValidate($action) {
        $retval = true;
        if ($this->required !== null) {
            foreach($this->required as $value) {
                if (!isset($this->data[$value])) {
                    return false;
                }
            }
        }
        if (isset($this->submitsrequired[$action])) {
            foreach($this->submitsrequired[$action] as $value) {
                if (!isset($this->data[$value])) {
                    return false;
                }
            }
        }
        foreach($this->data as $key => $value) {
            if(is_callable(array($this, "Validate_".$key))) {
                $retval &= call_user_func(array($this, "Validate_".$key),$action, $value);
            }
            else {
                $retval &= $this->Validate($action, $key, $value);
            }
        }
        return $retval;
    }



}