Re: QuickForm Validation
| From: | Bertrand Mansion | Date: | Sat, 12 Apr 2003 03:28:18 +0000 |
| Subject: | Re: QuickForm Validation | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-4811@lists.php.net to get a copy of this message | ||
<whovind@web-sters.com> wrote :
> Hello again. Still having problems and still trying to work them out.
>
> Given the following file the first form displays just fine. The second form
> shows the error for the name not being there. From what I can see there
> isn't any difference. Please. What am I missing???
>
> It would seem to me that validate() should "short circuit" on it's first
> call and it seems to do that on the first form.
>
> ANY insight appreciated.
>
>
> <?php
>
> require_once ("HTML/QuickForm.php");
> $form = new HTML_QuickForm('formOne', 'post');
> $form->addHeader('Step 1 - Name & Vehicles');
>
> $defaultValues['txtName'] = 'John Doe';
> $defaultValues['txtAddress1'] = '123 Any Street';
>
> $form->setDefaults($defaultValues);
>
> $form->addElement('text', 'txtName',
> 'Name:','size="60" class="formField"');
> $form->addElement('text', 'txtAddress1',
> 'Address:','size="60"
> class="formField"');
> $form->addElement('submit', null, 'Next');
>
> $form->addRule('txtName', 'Name is required.', 'required');
> $form->addRule('txtAddress1', 'Address is required.',
> 'required');
>
> $form2 = new HTML_QuickForm('formTwo', 'post');
>
> $form2->addHeader('Step 2 - Operators & Vehicle Use');
> $form2->addHeader('Main Operator');
> $form2->addElement('text', 'txtOp1Name',
> 'Name:','size="60"
> class="formField"');
> $form2->addElement('submit', null, 'Next');
>
> $form2->addRule('txtOp1Name', 'Name is required.',
> 'required');
>
>
> if (!isset($_SESSION["step"])) {
> $_SESSION["step"] = 1;
> }
>
> switch($_SESSION["step"]) {
> case 1:
> if($form->validate()) {
> $_SESSION['step']++;
> $form->freeze();
> }
> $form->display();
> break;
> case 2:
> if($form2->validate()) {
> $_SESSION['step']++;
> $form2->freeze();
> }
> $form2->display();
> break;
> default:
> print "I received step <b>'" . $_SESSION['step'] .
> "'</b> that
> dsp_autoQuote.php does not have a handler for.";
> $_SESSION['step']=1;
> break;
> }
>
> ?>
1. Why do you freeze the form, it's apparently useless in your case. Freeze
is used when you need to display the form in frozen mode.
2. If you really want to freeze, add an extra step.
3. Call display() only for the form that is not valid.
switch($_SESSION["step"]) {
case 1:
if($form->validate()) {
$_SESSION['step']++;
$currentForm = 'form2'
} else {
$currentForm = 'form';
}
break;
case 2:
if($form2->validate()) {
$_SESSION['step']++;
$currentForm = 'form3'
} else {
$currentForm = 'form2';
}
break;
(...)
}
${$currentForm}->display();
IMO, using include() would be faster than recreating every forms all the
time. And using a map for every form would more flexible, maybe this would
work (not tested, please report if it works):
$currentForm[0] = 'form';
$currentForm[1] = 'form2';
// Here test if $currentForm[$_SESSION["step"] exists
include($currentForm[$_SESSION["step"]].'.php');
if(${$currentForm[$_SESSION["step"]]}->validate()) {
$_SESSION['step']++;
include($currentForm[$_SESSION["step"]].'.php');
}
${$currentForm[$_SESSION["step"]]}->display();
Good luck,
Bertrand Mansion
Mamasam