Génération du formulaire dans une classe spécifique
Bonjour,
En prenant compte ma modeste expérience en ZF, je recommande les développeurs à utiliser des classe héritent depuis la classe Zend_Form.
Parce que:
- C'est plus flexible, au cas où je veux ajouter des fonctions propres à moi dans ma classe fils de Zend_Form.
- Ça me permet de spécifier un fichier .phtml (script XHTML) comme décorateur de mon formulaire au lien de se casser la tête avec les décorateurs des inputs propre a Zend.
Voila!
Cordialement.
1 pièce(s) jointe(s)
Mettre un décorateur XHTML propre
Bonjour,
Oui exactement :ccool:
Je m'explique ça peut-être intéressant pour les autres ;)
Je vous montre un exemple dont je travail actuellement (Search Lucene).
- Structure du projet (cf. PJ structure projet.jpg).
- Classe du formulaire :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| <?php
/**
* Class Views_Forms_AddForm
*
* Classe du formulaire Ajouter une entée dans la base de Lucene
*
* @author Ahmed Benlahsen <abenlahsen@gmail.com>
* @access public
*/
class Views_Forms_AddForm extends Zend_Form {
/**
* Nom du formalaire
* @var <string>
*/
private $_formName;
/**
* Action du formulaire
* @var <string>
*/
private $_action;
/**
* La clé de l'entrée dans Lucence
* @var <Zend_Form_Element_Text>
*/
private $_keywordValue;
/**
* Nom associé à la clé
* @var <Zend_Form_Element_Text>
*/
private $_nomValue;
/**
* Prenom associé à la clé
* @var <Zend_Form_Element_Text>
*/
private $_prenomValue;
/**
* Bouton submit
* @var <Zend_Form_Element_Submit>
*/
private $_submit;
/**
* Bouton reset
* @var <Zend_Form_Element_Reset>
*/
private $_reset;
/**
* Logger du projet
* @var <Zend_Log>
*/
private $_logger;
public function __construct(array $options) {
// Get looger object from registry
$this->_logger = Zend_Registry::get('logger');
// Check if params exists if not set default values
if( isset($options['name']) ){
$this->_formName = $options['name'];
} else {
$this->_formName = 'form';
}
if( isset($options['action']) ){
$this->_action = $options['action'];
} else {
$this->_action = '';
}
// Create elements objects
try{
$this->_keywordValue = new Zend_Form_Element_Text($options['keyValue']);
$this->_keywordValue->setLabel('Keywork :');
} catch (Zend_Form_Element_Exception $e){
$this->_logger->log($e->getMessage(), Zend_Log::ERR);
}
try{
$this->_nomValue = new Zend_Form_Element_Text($options['nomValue']);
$this->_nomValue->setLabel('Nom :');
} catch (Zend_Form_Element_Exception $e){
$this->_logger->log($e->getMessage(), Zend_Log::ERR);
}
try{
$this->_prenomValue = new Zend_Form_Element_Text($options['prenomValue']);
$this->_prenomValue->setLabel('Prenom :');
} catch (Zend_Form_Element_Exception $e){
$this->_logger->log($e->getMessage(), Zend_Log::ERR);
}
try{
$this->_submit = new Zend_Form_Element_Submit('submit');
$this->_reset = new Zend_Form_Element_Reset('reset');
} catch (Zend_Form_Element_Exception $e){
$this->_logger->log($e->getMessage(), Zend_Log::ERR);
}
// Init form
$this->init();
// Call parent construct
parent::__construct($options);
}
/**
* Function init
*
* Initialiser le formulaire:
* - Affecter le nom
* - Affecter le prenom
* - Affecter la method
* - Affecter le décorateur
* - Ajouter les elements au formulaire
*
* @author Ahmed Benlahsen <abenlahsen@gmail.com>
* @access public
*/
public function init(){
$this->setName($this->_formName);
$this->setAction($this->_action);
$this->setMethod('post');
$this->setDecorators(array(
array('ViewScript', array('viewScript' => 'forms/addform.phtml'))
));
$this->addElements(array(
$this->_keywordValue,
$this->_nomValue,
$this->_prenomValue,
$this->_submit,
$this->_reset
));
}
}
?> |
- Fichier addform.phtml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <form name="<?php echo $this->element->getName(); ?>" id="<?php echo $this->element->getId(); ?>" action="<?php echo $this->element->getAction(); ?>" method="<?php echo $this->element->getMethod(); ?>">
<table>
<tr>
<td><?php echo $this->element->keywordValue->renderLabel(); ?></td>
<td>
<?php echo $this->element->keywordValue->renderViewHelper(); ?>
<?php echo $this->element->keywordValue->renderErrors() ?>
</td>
</tr>
<tr>
<td><?php echo $this->element->nomValue->renderLabel(); ?></td>
<td>
<?php echo $this->element->nomValue->renderViewHelper(); ?>
<?php echo $this->element->nomValue->renderErrors() ?>
</td>
</tr>
<tr>
<td><?php echo $this->element->prenomValue->renderLabel(); ?></td>
<td>
<?php echo $this->element->prenomValue->renderViewHelper(); ?>
<?php echo $this->element->prenomValue->renderErrors() ?>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<?php echo $this->element->submit->renderViewHelper(); ?>
<?php echo $this->element->reset->renderViewHelper(); ?>
</td>
</tr>
</table>
</form> |
- L'appel dans le controller :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public function addAction(){
// If I don't use the AutoLoader of Zend we do this
Zend_Loader::loadClass('Views_Forms_AddForm', APPLICATION_PATH);
// Set params of my form
$options = array(
'name' => 'luceneAddForm',
'action' => $this->view->url(),
//'keyLabel' => 'keywordName',
'keyValue' => 'keywordValue',
//'nomLabel' => 'nomName',
'nomValue' => 'nomValue',
//'prenomLabel' => 'prenomLabel',
'prenomValue' => 'prenomValue'
);
// Instanciate my Form
$form = new Views_Forms_AddForm($options);
if( $this->getRequest()->isPost() ){// If form submited
// Get all form's values
$data = $this->getRequest()->getParams();
if( $form->isValid($data) ){// If is valid data
// My code here
} else {// If not => render form + Errors
$form->populate($data);
}
} else {// If not => set form to view
$this->view->form = $form;
}
} |
Enjoy!
Cordialement.