Jay
Jay

Reputation: 1033

Handle forms from a class, php

I have a quick question. I've been developing and designing an OO site, but there's one part of the whole process that so far isn't OOP. That is the script that handles the forms.

I've searched around and haven't found an answer so I'm not entirely sure if it is possible, hopefully you guys will be able to clarify for me.

My forms post to a standard script, like for example login.php, I would like my forms to post to a class, or more precisely, a method within that class. Is this at all possible?

Many thanks, Jay

Upvotes: 0

Views: 1112

Answers (2)

Erbureth
Erbureth

Reputation: 3423

You can use base class like this one that autoprocesses itself during constructor phase and inherit specific forms with specified fields. Don't forget some rendering code too. (here not displayed, as it is very system-specific.)

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;
    }



}

Upvotes: 0

Mihai Iorga
Mihai Iorga

Reputation: 39704

You cannot post directly to a class, but you can send parameters to a function in that class, for example:

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

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

or you can use the __construct() function:

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');

Upvotes: 4

Related Questions