info:html_quickform_exemple

PEAR html_quickform_exemple

| PEAR html_quickform_exemple
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>formulaire</title>
</head>
<body>
<?php
 
function traiteDonnees($values) {
echo "<pre>";
var_dump($values);
echo "</pre>";
}
    set_include_path(get_include_path() . ";c:\php\pear");
    require_once "HTML/QuickForm.php";
 
    $form = new HTML_QuickForm('frmTest', 'post');
    $form->addElement('text', 'Pseudo', 'Votre pseudo : ');
    $form->addElement('text', 'Nom', 'Votre nom : ');    
    $form->addElement('text', 'Email', 'Votre adresse email : ');    
    $options = array(
        'language'  => 'fr',
        'format'    => 'dMY',
        'minYear'   => 2001,
        'maxYear'   => 2005
    );
    $form->addElement('date', 'date', 'votre date de naissance : ', $options);
 
#amélioration de l'élément checkbox
$form->addElement('advcheckbox',
        'pomme',  // nom de l'element
        'Votre fruit préféré',   // texte avant l'élément
        'pomme',  // texte apres l'élément
        '', // attributs 
        array('sans pomme', 'avec pomme'));  //  valeur retournée case non cochée et case cochée
 
#cool: hierselect ajax style

$marque = array();
$modele = array();
 
$marque[0] = "Renault";
$marque[1] = "Peugeot";
$marque[2] = "Citroen";
 
$modele[0][0] = "Scenic";
$modele[0][1] = "Laguna";
$modele[0][2] = "Velsatis";
$modele[1][3] = "407";
$modele[1][4] = "607";
$modele[2][5] = "Xsara";
$modele[2][6] = "Picasso";
$modele[2][7] = "C5";
 
$sel =& $form->addElement('hierselect', 'voiture', 'Voiture : ');
$sel->setMainOptions($marque);
$sel->setSecOptions($modele);
 
#regles

    $form->addRule('Pseudo', 'Vous devez saisir un pseudo', 'required', '', 'client');
    $form->addRule('Nom', 'Vous devez saisir un nom', 'required', '', 'client');
    $form->addRule('Email', 'Vous devez saisir une adresse Email', 'required', '', 'client');
    $form->addRule('Pseudo', 'Votre pseudo doit avoir entre 6 caractères et 10 caractères', 'rangelength', array(6,10), 'client');
    $form->addRule('Email', 'Vous devez saisir une adresse email valide', 'email', '', 'client');
    $form->applyFilter('Nom','trim') ;
    $form->applyFilter('Pseudo','trim') ;
    $form->setRequiredNote('<span style="color: #ff0000">*</span> = champs obligatoires');
    $form->setJsWarnings('Erreur de saisie','Veuillez corriger');
 
    $form->addElement('reset', 'bouton_clear', 'Effacer');
    $form->addElement('submit', 'bouton_effacer', 'Envoyer');
 
 
if ($form->validate()) {
  $form->process('traiteDonnees', false);
       echo "Toutes les règles sont respectées<br>";
 
echo "marque =".$marque[$values['voiture'][0]];
echo "modele =".$modele[$values['voiture'][0]][$values['voiture'][1]];
 
traiteDonnees($values);
}
else {
  $form->display();
}
 
?>
</body>
</html>

pour voir le résultat: http://radeff.red/info/php/pearExemple.php

Encore quelques exemples

$form->addElement('password', 'pwd',      'Password:');
$form->addElement('select',   'dinner',   'Main Dish: ',
                  array('Beef', 'Chicken', 'Pork'));
$form->addElement('select',   'meat',     'Dish 2:',
                  array('cow' => 'Beef', 'hen' => 'Chicken', 'pig' => 'Pork'));
$form->addElement('textarea', 'comments', 'Comments: ',
                  'rows="5", cols="30"');
$form->addElement('textarea', 'moretalk', 'More Comments: ',
                  array('rows' => 5, 'cols' => 30));

source: http://devzone.zend.com/article/2996-Generating-and-Validating-Web-Forms-With-PEAR-HTML_QuickForm-part-2

// initialize group array
$group = array();

// add checkbox elements to group
$group[] = $form->createElement('checkbox', 'phone', null, 'Telephone');
$group[] = $form->createElement('checkbox', 'fax', null, 'Fax');
$group[] = $form->createElement('checkbox', 'email', null, 'Email');

// add group to form
$form->addGroup($group, 'contact', 'Contact methods:');

source: http://www.devarticles.com/c/a/Web-Graphic-Design/Using-HTML-Quickform-for-Form-Processing/5/

The following is the sample usage:
 
$form->addElement('radio','when','Departure:','Depart in the morning','morning');
$form->addElement('radio','when',null,'Depart in the afternoon','afternoon');
$form->addElement('radio','when',null,'Depart in the evening','evening');

The following is the sample HTML:

<tr>
<td align="right" valign="top"><b>Departure:</b></td>
<td valign="top" align="left"><input name="when" value="morning"
type="radio" id="qf_27dde5" /><label for="qf_27dde5">Depart in the
morning</label></td>
</tr>
<tr>
<td align="right" valign="top"><b></b></td>
<td valign="top" align="left"><input name="when" value="afternoon"
type="radio" id="qf_2316ca" /><label for="qf_2316ca">Depart in the
afternoon</label></td>
</tr>
<tr>
<td align="right" valign="top"><b></b></td>
<td valign="top" align="left"><input name="when" value="evening"
type="radio" id="qf_f7bf3f" /><label for="qf_f7bf3f">Depart in the
evening</label></td>
</tr>
$form->addElement('static','info','Information:',"Don't run with scissors");
     $form->addRule('telephone', 'Votre téléphone n\'est pas numérique', 'numeric', '', 'client');

pour vérifier un mot de passe avant envoi

source: http://devzone.zend.com/article/2996-Generating-and-Validating-Web-Forms-With-PEAR-HTML_QuickForm-part-2

<html>
<head></head>
<body>
<?php
// initialize HTML_QuickForm object
require_once 'HTML/QuickForm.php';
$form = new HTML_QuickForm();
 
// add input boxes
$form->addElement('password', 'pass', 'Password:', array('size' => 30));
$form->addElement('password', 'confirmpass', 'Confirm password:', array('size' => 30));
 
// add submit button
$form->addElement('submit', null, 'Submit');
 
// add validation rules
$form->addRule('pass', 'ERROR: Password missing', 'required');
$form->addRule('pass', 'ERROR: Password too short', 'minlength', 6);
$form->addRule('confirmpass', 'ERROR: Password missing', 'required');
$form->addRule(array('pass','confirmpass'), 'ERROR: Password mismatch', 'compare');
 
// change 'required' message
$form->setRequiredNote('Fields marked with <span style="color:red">*</span> are mandatory');
 
// print submitted values
// render and display the form
if ($form->validate()) {
  $form->freeze();
  echo 'You submitted the following data:';
} 
 
$form->display();
?>
</body>
</html>

ici le résultat en javascript

Voir aussi:

  • info/html_quickform_exemple.txt
  • Dernière modification : 2018/07/18 09:45
  • de radeff