Handle forms from a class, php
Get the solution ↓↓↓Solution:
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');
Answer
Solution:
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;
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: mark bundle as not supporting multiuse
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.