voila le micmac
<?php
class Formulaire {
// Toutes les propriétés du formulaire
private $name;
private $method; // POST et GET
private $formMethod = array(); // array (POST et GET)
private $formEnctype = array(); // array (application/x-www-form-urlencoded, multipart/form-data,text/plain)
private $formOptions = array();
private $InputOptions = array();
private $action;
private $value;
private $type; // text, password, button, submit, radio, checkbox
private $id;
private $class;
private $size;
private $maxlength;
private $cols;
private $rows;
private $enctype;
private $error;
private $optionsTextarea = array();
private $optionOptions = array();
private $fields = array();
private $infos = array();
private $separation;
######## On construit le formulaire:
######################################## Form Start #############################
public final function __construct($name, $method='', $action='', $formOptions=array(), $enctype='') { // champ name est obligatoire
// le nom du formulaire:
$this->name = $name;
// la methode
# on liste les differentes methodes
$this->formMethod = array('POST','GET');
// on supprime l'espace
$method = trim($method);
// si la method est vide on met POST par defaut sinon on la laisse
$method = (empty($method)) ? 'POST' : $method;
// si la method remplie ne contient pas le tableau formMethod (post, get) on met a defaut post
$method = (!in_array($method, $this->formMethod)) ? 'POST' : $method;
// on retourne la method
$this->method = $method;
//l'action
$action = (empty($action)) ? $_SERVER['PHP_SELF'] : $action;
$this->action = $action;
// formOptions
$this->formOptions = $formOptions;
$formOptionsArray = array(
"autocomplete" => array('on', 'off'),
"target" => array('_self', '_blank', '_parent', '_top'),
"novalidate" => array("novalidate"),
"DISABLED" => array(""),
); // DISABLED A SUPPRIMER
$this->formOptionsArray = $formOptionsArray;
$this -> infos ['name'] = $name;
$this -> infos ['method'] = $method;
$this -> infos ['action'] = $action;
//$this -> infos ['formOptions'] = $formOptions;
$this -> infos ['enctype'] = $enctype;
$formOptions = trim($formOptions);
// enctype
$formEnctype = array('application/x-www-form-urlencoded', 'multipart/form-data', 'text/plain');
$enctype = (empty($enctype)) ? 'application/x-www-form-urlencoded' : $enctype;
$enctype = (!in_array($enctype, $this->formEnctype)) ? 'application/x-www-form-urlencoded' : $enctype;
}
######################################## Form End #############################
######################################## Inputs Start #############################
public final function addInput($type, $name='', $value='', $InputOptions=array()){
$value = (empty($value)) ? $name : $value;
$id = (empty($id)) ? $name : $id;
$class = (empty($class)) ? $name : $class;
$size = (empty($size)) ? 20 : $size;
$maxlength = (empty($maxlength)) ? 50 : $maxlength;
$this->type = $type; //type
$this->name = $name; // nom
$this->value = $value; // valeur
$this->InputOptions = $InputOptions; // autres options des inputs
$this->id = $id; // id
$this->class = $class; // class
$this->size = $size; // size
$this->maxlength = $maxlength; // maxi
$this->Ioptions = $Ioptions;
$name = trim($name);
$this->fields[] = array(
'type' => $type,
'name' => $name,
'value' => $value,
'size' => $size,
'maxlength' => $maxlength,
'checked' => $checked,
'class' => $class
);
$InputOptionsArray = array(
'id' => array(''),
'class' => array(''),
'checked' => array('checked'),
'autocpmplete' => array('on', 'off'),
); // DISABLED A SUPPRIMER
$this->InputOptionsArray[] = $InputOptionsArray;
$InputOptions = trim($InputOptions);
} // end addInput function
######################################## Inputs End #############################
######################################## Build the Form Start #############################
function createForm(){
// on cherche si un input est de type file
$fileSearch = false;
foreach($this->fields as $field)
{
if($field['type'] == 'file')
{
$fileSearch = TRUE;
break;
}
}
// on verifie si les autres options de form existent
$OtherFormOptions = '';
echo 'this form options: '.var_dump($this->formOptions).'<br>';
foreach($this->formOptions as $key=>$value)
{
if(array_key_exists($key, $this->formOptionsArray) === true )
{
if(in_array($value,$this->formOptionsArray[$key]))
{
$OtherFormOptions .= ' '.$key.'="'.$value.'"';
}
else
{
$this->error[] = '<li>"'.$value.'" n est pas present</li>';
}
}
else
{
$this->error[] = '<li>"'.$key.'" n est pas present</li>';
//return false;
}
}
//$this->InputOptions = array('id' => $id, 'class' => 'o');
// on verifie si les autres options de input existent
$OtherInputOptions = '';
echo 'this input options: '.var_dump($this->InputOptions).'<br>';
foreach($this->InputOptions as $kkk=>$vvv)
{
//echo 'this input: '.$this->InputOptions.'<br>';
echo 'key is: '.$kkk.'<br>';
echo 'value is: '.$vvv.'<br>';
$OtherInputOptions .= $key.$value;
echo $OtherInputOptions;
}
// si on trouve que le type file existe on ajoute l'enctype
if($fileSearch)
{
// je n'ai pas fait d'espace entre action et OtherFormOptions pour ne pas avoir 2 espaces entre les 2
$form = '<form name="'.$this->infos['name'].'" method="'.$this->infos['method'].'" action="'.$this->infos['action'].'"'.$OtherFormOptions.' enctype="'.$this->infos['enctype'].'">';
}
else // sinon on enleve l'enctype
{
$form = '<form name="'.$this->infos['name'].'" method="'.$this->infos['method'].'" action="'.$this->infos['action'].'"'.$OtherFormOptions.'>';
}
foreach($this->fields as $field)
{
echo var_dump($field).'<br>';
//echo var_dump($field['type']).'<br>';
$fieldType = $field['type'];
$fieldName = $field['name'];
$fieldValue = $field['value'];
$fieldSize = $field['size'];
$fieldMax = $field['maxlength'];
$fieldClass = $field['class'];
$fieldId = $field['id'];
//addInput($type, $name='', $value='', $inputOptions=array()
Switch($fieldType)
{
case 'text':
$form .= '<input type="'.$fieldType.'" name="'.$fieldName.'" value="'.$fieldValue.'" '.$OtherInputOptions.' />';
break;
case 'password':
$form .= '<input type="'.$fieldType.'" name="'.$fieldName.'" value="'.$fieldValue.'" />';
break;
case 'submit':
$form .= '<input type="'.$fieldType.'" name="'.$fieldName.'" value="'.$fieldValue.'" />';
break;
case 'button':
$form .= '<input type="'.$fieldType.'" name="'.$fieldName.'" value="'.$fieldValue.'" />';
break;
case 'reset':
$form .= '<input type="'.$fieldType.'" name="'.$fieldName.'" value="'.$fieldValue.'" />';
break;
case 'file':
$form .= '<input type="'.$fieldType.'" name="'.$fieldName.'" />';
break;
case 'radio':
/*if($checked)
{
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" id="'.$id.'" class="' .$class.'" '.$checked.' />';
}
else
{
*/
$form .= '<input type="'.$fieldType.'" name="'.$fieldName.'" value="'.$fieldValue.'" />';
//}
break;
case 'checkbox':
/*if($checked)
{
echo '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" id="'.$id.'" class="' .$class.'" '.$checked.' />';
}
else
{
*/
$form .= '<input type="'.$fieldType.'" name="'.$fieldName.'" value="'.$fieldValue.'" />';
//}
break;
case 'hidden':
$form .= '<input type="'.$fieldType.'" name="'.$fieldName.'" value="'.$fieldValue.'" />';
break;
default:
$form .= '<input type="text" value="'.$fieldValue.'">';
break;
}
}
return $form;
} // end createForm
######################################## Get any Error Start #############################
public final function getError(){
//$ret = '';
echo '<br>nbre erreur: '.count($this->error).'<br>';
$ret_err = ''; // ret_err a rien
foreach($this->error as $erreur)// on loop pour chercher les erreurs
{
$ret_err .= $erreur; // on les recupere
}
echo '<ul><font color="#FF00EE">';
echo $ret_err.'<br>'; // on les affiche
echo '</font></ul>';
}
######################################## Get any Error End #############################
}// end class formulaire
######################################## Build the Form EnD #############################
######################################## Afficher les champs du formulaire #############################
//name, method, action, formOptions, enctype
//$frm1 = new Formulaire('monForm1', '', '', array('target' => '_self'));
$frm1 = new Formulaire('monForm1', '', '', array('autocomplet' => 'on', 'target' => '_self', 'DISABLED' => '','novalidate' => 'novalidate'));
//$type, $name='', $value='', $InputOptions=array()
$frm1->addInput('text', 'nomText1', 'valTxt', array('id' => 'myID', 'class' => 'myClass')); // text
$frm1->addInput('text', 'nomtext'); // champ text
$frm1->addInput('text', 'nomtext2', '');
$frm1->addInput('file', 'nomFile', 'valeur'); // file upload
$frm1->addInput('text', 'nomtext2', 'valeur'); // parce que je n'ai pas fait un <br> apres ce champs en create form'
$frm1->addInput('radio', 'nomtext2', 'valeur'); // parce que je n'ai pas fait un <br> apres ce champs en create form'
echo $frm1->createForm();
echo $frm1->getError();
?>