Génération de formulaires dynamique, avec regénération sur la page action

Contenu du snippet

Voila une classe pour générer des <form> et <input> proprement, sans attributs inexistants et récupérables sur la page action suivante !

J'ai commenté comme je pouvais dans la source :)

Exemple :

<?php
include 'generateForm.class.php';

$form = new generateForm('index2.php', 'myForm', 'post', array('title' => 'data', 'azd' => 'dddd'));
$form->addInput('texzdt', 'thom', array('golef' => 'afgepogh'), 40);
$form->addInput('texzdt', 'thom4', array('golef' => 'afgepogh'), 20);
$form->addInput('texafazfzdt', 'thom5', array('value' => 'afgezzzzzzzzzzzzzzzzzzzpogh'), 40);
$form->addInput('texzdt', 'thom6', array('golef' => 'afgepogh'), 50);
$form->addInput('texzdt', 'thom7', array('golef' => 'afgepogh'), 110);
$form->addInput('submit', 'thom2', array('value' => 'afgepogh'), 40);
$form->addInput('textarea', 'thom3', array('value' => 'afgepogh'), '5*10');

echo $form->showForm();
echo $form->showInput('thom');
echo '<br>';
echo $form->showInput('thom2');
echo '<br>';
echo $form->showInput('thom3');
echo '<br>';
echo $form->showInput('thom4');
echo '<br>';
echo $form->showInput('thom5');
echo '<br>';
echo $form->showInput('thom6');
echo '<br>';
echo $form->showInput('thom7');
echo $form->endForm();
?>

donc en gros, on genere dans la classe le form, les inputs, et apres on les affiche ou on veut :)

Sur la page suivante, si on a laissé $sendCatchValues à true :

<?php
include 'generateForm.class.php';
$form = new regenerateSameForm($_POST['regenerateForm']);
echo $form->showForm();
echo $form->showInput('thom');
echo '<br>';
echo $form->showInput('thom2');
echo '<br>';
echo $form->showInput('thom3');
echo '<br>';
echo $form->showInput('thom4');
echo '<br>';
echo $form->showInput('thom5');
echo '<br>';
echo $form->showInput('thom6');
echo '<br>';
echo $form->showInput('thom7');
echo $form->endForm();

?>

Ca refait le meme form :)

Voila je suis ouvert à toute amélioration ou continuation de ce projet :)

Source / Exemple :


<?php

/** class generateForm - Generates dynamic forms
*

  • This class will generate inputs in valid XHTML strict 1.0 on-the-fly with all attributes that you want
  • (they are checked in the script, if the are not valid, they are not shown).
  • @author Thomas Cantonnet <hobzdatiger at gmail dot com>
  • @copyright Copyright © 2005 , Thomas Cantonnet
  • @version 1.0
  • /
class generateForm { /**
  • @var string $form beginning of the form
  • @access public
  • /
public $form; /**
  • @var string $endForm end of the form
  • @access public
  • /
public $endForm; /**
  • @var integer $numberInputs number of inputs in the form
  • @access public
  • /
public $numberInputs = 0; /**
  • @var array $inputs array containing every inputs you have set
  • @access public
  • /
private $inputs = array(); /**
  • @var integer $defaultSize beginning of the form
  • @access private
  • /
private $defaultSize = 20; /**
  • @var string $form beginning of the form
  • @access private
  • /
private $defaultTextareaRows = 20; /**
  • @var string $form beginning of the form
  • @access private
  • /
private $defaultTextareaCols = 100; /**
  • @var string $form beginning of the form
  • @access private
  • /
private $typeOfInput = 50; /**
  • @var string $data contains information of the form for regeneration
  • @access private
  • /
private $data; /**
  • @var boolean $sendCatchValues true if ou want to regenerate the form
  • @access private
  • /
private $sendCatchValues; /**
  • @var integer $generatingForm verifies if we are currently generating a form
  • @access private
  • /
private $generatingForm = 0; /**
  • @param array $myAttributes array like this -> array('inputAttribute' => 'Value')
  • @param string $action the page that is going to be accessed when the form is submitted, default = $PHP_SELF
  • @param string $name name of the form you are going to generate
  • @param string $method POST or GET, default is POST
  • @param bool $sendCatchValues true if you want to regenerate form after submit
  • @access public
  • @return void
  • @desc will generateForm and set $this->form to contain the header of your form
  • /
public function generateForm($action = '', $name = 'generatedForm', $method = 'POST', $myAttributes = '', $sendCatchValues = true) { if($this->generatingForm > 0) return; if(is_array($myAttributes)) { foreach($myAttributes as $attribute => $value) { if(in_array($attribute, array('accept', 'accept-charset', 'class', 'enctype', 'lang', 'style', 'target', 'title'))) { $allowedAttributes .= $attribute.'="'.$value.'" '; $forRegeneration .= $attribute. '[*]' .$value. '[*]'; } } $allowedAttributes = substr($allowedAttributes, 0, strlen($allowedAttributes)-1); $forRegeneration = substr($forRegeneration, 0, strlen($forRegeneration)-3); $forRegeneration = ',' .$forRegeneration; } if(!in_array(strtoupper($method), array('POST', 'GET'))) $method = 'POST'; $this->form = '<form action="'.$action.'" name="'.$name.'" method="'.$method.'" '.$allowedAttributes.">\n"; $this->endForm = '</form>'; if($sendCatchValues == true) { $this->sendCatchValues = true; $this->data = $action. ',' .$name. ',' .$method. '' .$forRegeneration. '|'; } $this->generatingForm++; } /**
  • @param string $type will set the type of the input you ant to generate
  • @param string $name will set the name of your input, highly recommanded to set it properly
  • @param array $myAttributes will put every attributes you want in an input if it's valid
  • @param integer $size will set the size of the input if possible
  • @return void
  • /
public function addInput($type, $name, $myAttributes, $size = '') { if(!in_array($type, array('button', 'checkbox', 'file', 'hidden', 'image', 'password', 'radio', 'reset', 'submit', 'text', 'textarea'))) { $type = 'text'; } if(in_array($type, array('text', 'password'))) { $size = intval($size); if($size <= 0 || $size > 100) { $sizeOk = ' size="'.$this->defaultSize.'"'; } else { $sizeOk = ' size="'.$size.'"'; } } if($type == 'textarea') { if(strpos($size, '*') === false) { $rows = $this->defaultTextareaRows; $cols = $this->defaultTextareaCols; $sizeOk = ' rows="'.$this->defaultTextareaRows.'" cols="'.$this->defaultTextareaCols.'"'; } else { $size2 = explode('*', $size); $size2[0] = intval($size2[0]); $size2[1] = intval($size2[1]); if(!is_int($size2[0])) { $rows = $this->defaultTextareaRows; } if(!is_int($size2[1])) { $cols = $this->defaultTextareaCols; } else { $rows = $size2[0]; $cols = $size2[1]; } $sizeOk = ' rows="'.$rows.'" cols="'.$cols.'"'; } } if(is_array($myAttributes)) { switch($type) { case 'button' : $authorized = array('class', 'lang', 'style', 'title', 'value'); break; case 'checkbox' : $authorized = array('checked', 'class', 'lang', 'style', 'title', 'value'); break; case 'file' : $authorized = array('accept', 'class', 'lang', 'style', 'title', 'value'); case 'hidden' : $authorized = array('class', 'lang', 'style', 'title', 'value'); break; case 'image' : $authorized = array('align', 'src', 'class', 'lang', 'style', 'title', 'value'); break; case 'password' : $authorized = array(' maxlength', 'class', 'lang', 'style', 'title', 'value'); break; case 'radio' : $authorized = array('checked', 'class', 'lang', 'style', 'title', 'value'); break; case 'reset' : $authorized = array('class', 'lang', 'style', 'title', 'value'); break; case 'submit' : $authorized = array('class', 'lang', 'style', 'title', 'value'); break; case 'text' : $authorized = array('class', 'lang', 'maxlength', 'style', 'title', 'value'); break; case 'textarea' : $authorized = array('class', 'lang', 'style', 'title', 'cols', 'rows', 'wrap', 'value'); break; default; } foreach($myAttributes as $attribute => $value) { if(in_array($attribute, $authorized)) { $allowedAttributes .= ' '.$attribute.'="'.$value.'"'; $forRegenerate .= $attribute. '[;]' .$value. '[;]'; if($type == 'textarea' && $attribute == 'value') { $insideTextareaValue = $value; } } } $forRegenerate = substr($forRegenerate, 0, strlen($forRegenerate)-3); $forRegenerate = ',' .$forRegenerate; } $this->numberInputs++; if($type == 'textarea') { $this->typeOfInput = 'textarea '; $closeTag = '>'.$value.'</textarea>'; } else { $this->typeOfInput = 'input '; $showType = 'type="'.$type.'" '; $closeTag = ' />'; } $this->inputs[$name] = '<'.$this->typeOfInput.''.$showType.'name="'.$name.'"'.$sizeOk.''.$allowedAttributes.''.$closeTag; if($this->sendCatchValues == true) { if($size != '') $size = ',' .$size; $this->data .= $type. ',' .$name. ''.$size. '' .$forRegenerate. '[$]'; } } /**
  • @return string or bool
  • @desc will return the beginning of the form if it is set
  • /
public function showForm() { if(isset($this->form)) return $this->form; else return false; } /**
  • @return string or bool
  • @desc will return the input you have chosen, if it is set
  • /
public function showInput($name) { if(isset($this->inputs[$name])) return $this->inputs[$name]."\n"; else return false; } /**
  • @return string
  • @desc will return the hidden input containing the information to regenerate form if $sendCatchValues = true
  • /
public function endForm() { if($this->sendCatchValues == true) return '<input type="hidden" name="regenerateForm" value="' .base64_encode(substr($this->data, 0, strlen($this->data)-3)). " />\n" .$this->endForm; else return $this->endForm; } } class regenerateSameForm extends generateForm { function transform_keys($array, $new_array) { $count = count($array); if($count == 0) return; for($i=0; $i<=$count;$i++) { if($array[$i] != '' && $array[($i+1)] != '') $new_array[$array[$i]] = $array[($i+1)]; $i++; } return $new_array; } function regenerateSameForm($data) { $data = base64_decode($data); $data = explode('|', $data); $formFirstAttributes = explode(',', $data[0]); // contains default form's attributes $myFormAttributes = explode('[*]', $formFirstAttributes[count($formFirstAttributes)-1]); // contains custom form's attributes $count = count($myFormAttributes); for($i=0;$i<=$count;$i++) { if(!empty($myFormAttributes[($i+1)])) $formAttributes[$myFormAttributes[$i]] = $myFormAttributes[($i+1)]; $i++; } $this->generateForm($formFirstAttributes[0], $formFirstAttributes[1], $formFirstAttributes[2], $formAttributes); $inputs = explode('[$]', $data[1]); // contains every input $count = count($inputs); $i = 0; foreach($inputs as $attributes) { $inputAttributes_{$i} = explode(',', $attributes); // contains each attribute in an array if(is_array($inputsAttributes_{$i})) { foreach($inputAttributes as $key => $value) { if(empty($inputsAttributes_{$i}[$key])) unset($inputsAttributes_{$i}[$key]); } } $i++; } for($i=0; $i<=$count;$i++) { if(strpos($inputAttributes_{$i}[count($inputAttributes_{$i})-1], '[;]') === FALSE) { } else { $myInputAttributes_{$i} = explode('[;]', $inputAttributes_{$i}[count($inputAttributes_{$i})-1]); // contains each myAttributes in an array } } for($i=0;$i<=$count;$i++) { if($inputAttributes_{$i}[0] != '' && $inputAttributes_{$i}[1] != '' && $inputAttributes_{$i}[2] != '') $this->addInput($inputAttributes_{$i}[0], $inputAttributes_{$i}[1], $this->transform_keys($myInputAttributes_{$i}, $InputAttributesOk_{$i}), $inputAttributes_{$i}[2]); } } } ?>

Conclusion :


Petite précision : l'attribut value dans textarea n'existe pas, mais il est possible de le mettre. Quand celui ci est mis dans l'array, le parametre value est supprimé, on garde la valeur du value et on le met entre lle <textarea> et le </texarea> ;)

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.