Smarty & Quickform Labelling Required Template Groups
| From: | Colin Barrett | Date: | Fri, 29 Oct 2004 00:44:05 +0000 |
| Subject: | Smarty & Quickform Labelling Required Template Groups | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-11933@lists.php.net to get a copy of this message | ||
I'm working on a form that requires a user to enter at least one telephone
number (either home, work, or cell), and I would like to place a symbol
beside each label to denote that only one is required, for example:
* Required field
+ Only one field is required
* Name: _________
+ Home Phone: __________
+ Work Phone: __________
+ Cell Phone: __________
Using the code below, the Smarty debugger shows me that:
Contact => required => 1
Contact => required => Home => required // (blank)
Contact => required => Work => required // (blank)
Contact => required => Cell => required // (blank)
I was thinking it would be nice to be able to access the required field
for the group when I'm specifying the label for each element. Is it
possible to specify the Required Template with something like:
$renderer->setRequiredTemplate(
'{if $required}*
{elseif $parent->required}+
{/if}
{$label}'
);
I know that doesn't work, but I hope it's clear enough to get across
the idea of what I want to achieve. Or is there already a strategy for
achieving this look?
Below please find a couple of fragments of my PHP code and Smarty
template. I hope they're sufficient.
Thanks in advance.
PHP Fragments
-------------
$gr[] =& HTML_QuickForm::createElement('text', 'Home', 'Home
Phone');
$gr[] =& HTML_QuickForm::createElement('text', 'Work', 'Work
Phone');
$gr[] =& HTML_QuickForm::createElement('text', 'Cell', 'Cell
Phone');
$form->addGroup($gr, 'Phone', 'Contact Information', ' ');
$form->addGroupRule('Phone', 'Atleast one phone number is required',
'required', null, 1);
$tpl =& new Smarty;
$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($tpl);
$renderer->setRequiredTemplate('{if $required}<font
color="red">*</font>
{/if}{$label}');
$form->accept($renderer);
$tpl->assign('form', $renderer->toArray());
$tpl->display('fragment.tpl');
Template Fragment
-----------------
<table>
<tr>
<th>{$form.Phone.Home.label}</th>
<td>{$form.Phone.Home.html}</td>
</tr>
<tr>
<th>{$form.Phone.Work.label}</th>
<td>{$form.Phone.Work.html}</td>
</tr>
<tr>
<th>{$form.Phone.Cell.label}</th>
<td>{$form.Phone.Cell.html}</td>
</tr>
</table>