Re: QuickForm question
| From: | Bertrand Mansion | Date: | Tue, 18 Jun 2002 13:15:32 +0000 |
| Subject: | Re: QuickForm question | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-1706@lists.php.net to get a copy of this message | ||
le 18/06/02 14:48, Peter Bowyer à peter@mapledesign.co.uk a écrit :
> Hi,
>
> How do I specify extra attributes for a QuickForm element? for instance I
> have:
> $attr = 'class="foo" onMouseOver="doBar();"';
> and I want to assign this to one of the elements. I'm looking for a method
> like setAttributes() but can't find it :-(
>
> FYI I'm using QuickForm the OO way, and building the form from a PHP array:
> <?php
> $lang['form']['test'] = 'A test textarea for you';
> $lang['form']['test2'] = 'A text input';
>
> $forms[] = array(
> 'name' => 'test', // Name for input
> 'type' => 'textarea', // e.g. input, textarea etc
> 'validation' => '', // Regex to be used
> 'attributes' => 'style="border: 1px solid #FFCCCC;"', //
> Extra attributes for the form tag
> 'width' => 60,
> 'height' => 6,
> 'max_size' => '', // Max length
> 'error_message' => '', // Comes from language source
> 'required' => '', // true or false
> 'default' => 'This is default text', //
> );
> $forms[] = array(
> 'name' => 'test2', // Name for input
> 'type' => 'text', // e.g. input, textarea etc
> 'validation' => '', // Regex to be used
> 'attributes' => 'style="border: 1px solid #00CCCC;"', //
> Extra attributes for the form tag
> 'width' => 20,
> 'max_size' => '', // Max length
> 'error_message' => '', // Comes from language source
> 'required' => '', // true or false
> 'default' => 'Text', //
> );
>
> foreach($forms as $el){
> $form[$el['name']] = &HTML_QuickForm::createElement($el['type']);
> if ($el['width'] || $el['height']) {
> switch($el['type']){
> case 'textarea':
> $form[$el['name']]->setCols($el['width']);
> $form[$el['name']]->setRows($el['height']);
> break;
> case 'checkbox':
> case 'radio':
> break;
> default:
> $form[$el['name']]->setSize($el['width']);
> } // switch
> // $form[$el['name']]->setSize($el['width']);
> }
> if ($el['max_size']) {
> $form[$el['name']]->setMaxLength($el['max_size']);
> }
> $form[$el['name']]->setName($el['name']);
> if ($el['default']) {
> $form[$el['name']]->setValue($el['default']);
> }
>
> $output[$el['name']]['HTML'] = $form[$el['name']]->toHtml();
> $output[$el['name']]['lang'] =
> $lang['form'][$el['name']];
> }
>
> // Print it to see what we've got
> foreach($output as $out){
> print '<p>' . $out['lang'] . '<br />' .
> $out['HTML'] . '</p>';
> }
> ?>
If you want to do this, you will have to make your own little parser a bit
like you already did.
But you'd better make a distinction between the elements and their
attributes.
You could probably do something like this:
$txtAttributes = array( 'maxlength' => 30,
'style' => 'border: 1px solid #FFCCCC;');
Then your elements :
$elementArr = array('name' => 'test1', 'type' =>
'text', 'attributes' =>
$txtAttributes, 'rule' => 'required', 'rulemsg' => 'element
is required'
...);
Probably, you will make a big array with all your element arrays.
then with your own parser (a foreach loop), you walk the elements array and
when you meet a new element you do $form->addElement($name, $type, $label,
$attributes); and if you want some rules, you just do $form->addRule($name,
$rule, $rulemsg);
BTW, QuickForm elements all extend HTML_Common which has the following
methods :
- setAttributes
- updateAttributes
- removeAttribute and so on...
This way, you can do: $myelement->setAttributes( array or string of
attributes) whenever you want to modify your element. Methods like
setMaxlength, setSize and the rest are just convenience accessors to make it
easier to set these default parameters.
All in all, what you want to do is really possible with QuickForm but will
require a bit more effort from the beginning by working on your parser and
calling the right methods.
If I were you, I would also use a form object. I would create a new form
object, then create my elements individually, then call
$myFormObject->addElement($myElement); just to make sure values and
validation work. I don't think it is possible to validate an element when it
is not in a form object.
Bertrand Mansion
Mamasam