Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 3.2.14

Bug #253 Add a group of Radio-Buttons to a Static Smarty template on different locations
Submitted: 2003-11-14 13:20 UTC Modified: 2004-01-26 07:07 UTC
From: wentz at gmx dot de Assigned: ths
Status: Closed Package: HTML_QuickForm
PHP Version: 4.3.3 OS: Gentoo Linux w/ Kernel 2.4.20
Roadmaps: (Not assigned)    
Subscription  


 [2003-11-14 13:20 UTC] wentz at gmx dot de
Description: ------------ The goal is to have 3 Radio Buttons, not in a row but placed each on a different location. php-example: <php> $pic1[] = &HTML_QuickForm::createElement('radio', null, null, 'No Pic', 'no'); $pic1[] = &HTML_QuickForm::createElement('radio', null, null, 'Upload Pic', 'upload'); $pic1[] = &HTML_QuickForm::createElement('radio', null, null, 'Use existing', 'use'); $form->addGroup($pic1, 'pic1', 'Picture 1:'); $form->addElement('file', 'pic1upload', 'Upload:'); </php> template-example: {form.pic1.no.html}<br> {form.pic1.upload.html}<br> {form.pic1upload.html} {form.pic1.use.html} It seems like this is not possible in with the SmartyStatic-Renderer, {form.pic1.html} works, but i need the radio-buttons seperated. Stephan Reproduce code: --------------- php-example: <php> $pic1[] = &HTML_QuickForm::createElement('radio', null, null, 'No Pic', 'no'); $pic1[] = &HTML_QuickForm::createElement('radio', null, null, 'Upload Pic', 'upload'); $pic1[] = &HTML_QuickForm::createElement('radio', null, null, 'Use existing', 'use'); $form->addGroup($pic1, 'pic1', 'Picture 1:'); $form->addElement('file', 'pic1upload', 'Upload:'); </php> template-example (method 1): {form.pic1.no.html}<br> {form.pic1.upload.html}<br> {form.pic1upload.html} {form.pic1.use.html} template-example (method 2): {form.pic1.0.html}<br> {form.pic1.1.html}<br> {form.pic1upload.html} {form.pic1.2.html} Expected result: ---------------- The Radio-Button-Elements should show up like other Elements. Actual result: -------------- The Elements aren't shown.

Comments

 [2003-11-14 13:47 UTC] avb
Just a comment: it is better to add such radios not as a group, but as separate elements: $form->addElement('radio', 'pic1', null, 'No Pic', 'no'); $form->addElement('radio', 'pic1', null, 'Upload Pic', 'upload'); $form->addElement('radio', 'pic1', null, 'Use existing', 'use'); Unfortunately, ArraySmarty does not handle this case as well.
 [2003-11-14 14:57 UTC] ths at php dot net
Alexey, to provide an easy way to change from an dynamic to an static way of template rendering and just because radios are groups logical and in layout, the "better" way should be use groups for radios. This Bug is the same in ITStatic. So we should decide how to handle grouped radios in static renderers. I like the idea to use the value as key here. This is more intuitive then an numric key. {$form.pic1.no.html} / {form_pic1_no_html}
 [2003-11-14 18:25 UTC] ths at php dot net
This should fix the problems adressed in these bug. cvs -q diff -uw Index: ArraySmarty.php =================================================================== RCS file: /repository/pear/HTML_QuickForm/QuickForm/Renderer/ArraySmarty.php,v retrieving revision 1.3 diff -u -w -r1.3 ArraySmarty.php --- ArraySmarty.php 4 Nov 2003 10:53:03 -0000 1.3 +++ ArraySmarty.php 14 Nov 2003 23:22:00 -0000 @@ -85,7 +85,13 @@ * Current element index * @var integer */ - var $_elementIdx; + var $_elementIdx = 0; + + /** + * If elements have been added with the same name + * @var array + */ + var $_duplicateElements = array(); /** * The current element index inside a group @@ -119,6 +125,20 @@ } // end constructor + function startForm(&$form) + { + parent::startForm($form); + + $this->_formName = $form->getAttribute('name'); + + if (count($form->_duplicateIndex) > 0) { + // Take care of duplicate elements + foreach ($form->_duplicateIndex as $elementName => $indexes) { + $this->_duplicateElements[$elementName] = 0; + } + } + } // end func startForm + function renderHeader(&$header) { if ($name = $header->getName()) { @@ -165,13 +185,39 @@ // create a simple element key $ret['key'] = $ret['name']; - if (strstr($ret['key'], '[')) { - preg_match('/\\[([^]]*)\\]/', $ret['key'], $matches); + + // grouped elements + if (strstr($ret['key'], '[') or $this->_currentGroup) { + // TODO: this should scale... + preg_match('/([^]]*)\\[([^]]*)\\]/', $ret['key'], $matches); + // pseudo group element + if (empty($this->_currentGroup)) { + if ($matches[2] != '') { + $newret['key'] = $matches[1]; + $newret[$matches[2]] = $ret; + $ret = $newret; + } else { $ret['key'] = $matches[1]; - if (empty($ret['key'])) { + } + // real group element + } else { + $ret['key'] = $matches[2]; + if ($ret['key'] == '') { + if ($ret['type'] == 'radio') { + $ret['key'] = $ret['value']; + } else { $ret['key'] = $this->_groupElementIdx++; } - } elseif (empty($ret['key'])) { + } + } + // element is a duplicate + } elseif (isset($this->_duplicateElements[$ret['key']])) { + $newret['key'] = $ret['key']; + $newret[intval($this->_duplicateElements[$name])] = $ret; + $ret = $newret; + $this->_duplicateElements[$name]++; + // element has no name + } elseif ($ret['key'] == '') { $ret['key'] = 'element_' . $this->_elementIdx; } $this->_elementIdx++; @@ -194,9 +240,13 @@ if (is_array($this->_currentGroup) && ('group' != $elAry['type'])) { $this->_currentGroup[$key] = $elAry; } else { + if (isset($this->_ary[$key])) { + $this->_ary[$key] = $this->_ary[$key] + $elAry; + } else { $this->_ary[$key] = $elAry; } } + } /**
 [2003-11-15 04:02 UTC] avb
The patch mostly works, but throws a lot of notices like <b>Notice</b>: Undefined variable: name in <b>...\HTML\QuickForm\Renderer\ArraySmarty.php</b> on line <b>216</b><br /> <b>Notice</b>: Undefined index: in <b>...\HTML\QuickForm\Renderer\ArraySmarty.php</b> on line <b>216</b><br /> <b>Notice</b>: Undefined variable: name in <b>...\HTML\QuickForm\Renderer\ArraySmarty.php</b> on line <b>218</b><br /> <b>Notice</b>: Undefined offset: 2 in <b>...\HTML\QuickForm\Renderer\ArraySmarty.php</b> on line <b>204</b><br /> Besides, I don't like the idea of accessing the private _duplicateIndex property of HTML_QuickForm. As for why I suggested adding the radio buttons separately: this is to be consistent with dynamic renderers behaviour, they are only able to render the grouped elements near each other. Your patch also uses numbers for indexes when radios are not grouped and values for indexes when they are grouped. Is it intended?
 [2003-11-15 08:19 UTC] ths at php dot net
Uhps, sorry for that. Changed. Can we have a public property/method for _duplicateIndex? Get this from ITStatic renderer. I will not change my form definition, when I switch from dynamic to static with elements grouped to render them near each other. But I will have the option to arrange them flexible. I have no problem with radio-value as key also for non grouped radios. The question is: Should't we provide the same naming rules for similar renderes?
 [2003-11-21 07:41 UTC] avb
Fixed in CVS. I applied the patch Thomas sent me, it seems to fix the problem.
 [2004-01-26 07:07 UTC] wentz at gmx dot de
hi, i'm the original bug reporter and it seems like this one doesn't work as expected yet (just found out using it). at first i found out that (in this example) always 'use' is selected as default, even when no default is set. when setting a default (for example $def['pic1'] = 'no';) still 'use' is selected. now for the strange part: when i add a {$form.pic1.html} to the template everything works fine, 'no' is selected in both places (the group-output and the single-element-output). maybe there's some sort of calculation that's only going on when add the group-html?
 [2005-06-05 08:33 UTC] sk at bouncemedia dot de
hi, i have thes same problem as the original reporter. i installed the bugfix, but the radio buttons wont show up. i am using itstatic with sigma templat.
 [2005-08-19 10:04 UTC] mdjones0978-php at yahoo dot com
Well none of what I saw here helped me, but this is what I learned. If you are going to use radio buttons with Smarty, you need to use a group. Here is the PHP code: $repeat = array(); $repeat[] = $form->addElement('radio', 'repeat', 'none', null, 'none'); $repeat[] = $form->addElement('radio', 'repeat', 'repeat', null, 'repeat'); $repeat[] = $form->addElement('radio', 'repeat', 'repeaton', null, 'reapeaton'); $form->addGroup($repeat, 'repeat', '', '<br />'); $form->addElement('select', 'repeatevery', '', array('Every' => 'Every', 'Every Other' => 'Every Other')); $form->addElement('select', 'repeateverywhat', '', array('Day' => 'Day', 'Week' => 'Week', 'Month' => 'Month', 'Year' => 'Year', 'MWF' => 'Mon, Wed, Fri', 'TuesThurs' => 'Tue & Thu', 'Weekdays' => 'Mon Thru Fri', 'Weekends' => 'Sat & Sun')); $enddate = array(); $enddate[] = $form->addElement('radio', 'ends', 'none', null, 'none'); $enddate[] = $form->addElement('radio', 'ends', 'endon', null, 'endon'); $form->addGroup($enddate, 'ends', '', '<br />'); $form->addElement('date', 'enddate', 'Date:', array('format' => 'F d, Y', 'minYear' => date('Y'), 'maxYear' => date('Y')+10, 'addEmptyOption' => true, 'emptyOptionText' => ' ')); Here is the Template code: <tr> <td class="label">{$form.newshowtime.label}</td> <td class="element">{$form.newshowtime.html}</td> </tr> <tr> <TD CLASS='label'>Repeating:</TD> <TD CLASS='element'> {$form.repeat.none.html} This event does not repeat <BR>{$form.repeat.repeat.html} Repeat {$form.repeatevery.html} {$form.repeateverywhat.html}</TD> </tr> <tr> <TD CLASS='label'>End Date:</TD> <TD CLASS='element'> {$form.ends.none.html} No End Date <BR>{$form.ends.endon.html} Repeat until {$form.enddate.html}</TD> </tr>