I made a formbuilder, and now im thinking of validation and showing a form with the wrong values.
What is the right way to do this? I was thinking of making a new validation class, and a setter in the field class to decide wich validation you want, and then create the object.
And how can i return the wrong inputs, so the user has the chance to modify them? All i could think of, is making a setter for the value, so i can easily modify it. cause how my class is designed now, its not possible
EDIT: i know its not a finished class, and i have not commented the class. That's because its a WIP, and i need some tips before i continue.
<?php
/**
* form.class.php
**/
include 'form/field.class.php';
include 'form/input.class.php';
class Form {
private $_action;
private $_method;
protected $_formHtml;
protected $_atributes;
protected $_fields;
public function __construct($action,$method) {
$this->_action = $action;
$this->_method = $method;
}
protected function openForm() {
$this->_formOpen = '<form action="'.$this->_action.'" method="'.$this->_method.'" ';
if(isset($this->_atributes)) {
foreach($this->_atributes as $key => $value) {
$this->_formOpen .= ' '.$key.'="'.$value.'"';
}
}
$this->_formOpen .= '>'."
";
return $this->_formOpen;
}
protected function closeForm() {
$this->_formClose = '</form>'."
";
return $this->_formClose;
}
public function setAtribute($array) {
foreach($array as $key => $value) {
$this->_atributes[$key] = $value;
}
}
public function generateForm($array) {
echo $this->openForm();
foreach($array as $key) {
$key->generateField();
$key->display();
}
echo $this->closeForm();
echo $this->_formHtml;
}
public function showValues($submit) {
if(isset($_POST[$submit])) {
foreach($_POST as $key => $value) {
if($key !== $submit)
echo ''.$key.' : '.$value.'<br>';
}
}
}
}
?>
<?php
/**
* field.class.php abstract class
**/
abstract class Field {
protected $_error;
abstract protected function generateField();
abstract public function display();
abstract public function returnHtml();
public function setError($error) {
$this->_error = $error;
}
public function checkErrors() {
}
public function returnErrors() {
return $this->_error;
}
}
?>
<?php
/**
* input.class.php
**/
class Input extends Field {
protected $_elements;
var $_html;
public function __construct($array) {
foreach($array as $key => $value) {
$this->_elements[$key] = $value;
}
}
public function generateField(){
$this->_html = '<input';
foreach($this->_elements as $key => $value) {
$this->_html .= ' '.$key.'="'.$value.'"';
}
$this->_html .= '>'."
";
}
public function returnHtml() {
return $this->_html;
}
public function display() {
echo $this->_html;
}
}
?>
<?php
/**
* index.php Voorbeeld
**/
require 'classes/form.class.php';
$username = new Input(array(
'type' => 'text',
'name' => 'username',
'placeholder' => 'Username'
));
$email = new Input (array(
'type' => 'email',
'name' => 'email',
'placeholder' => 'Email',
));
$password = new Input(array(
'type' => 'password',
'name' => 'password',
'placeholder' => 'Password'
));
$submit = new Input(array(
'type' => 'submit',
'name' => 'submit_login',
'value' => 'Login'
));
$form = new Form('','post');
$form->setAtribute(array('id' => 'test','class' => 'bla'));
$form->generateForm(array($username,$email,$password,$submit));
$form->showValues('submit_login');