Home » HTML » HTML_Template_Flexy » Bug #9591
a patch to allow radiobuttons not to have id
Details
| Request #9591 | a patch to allow radiobuttons not to have id |
|---|---|
| Submitted | 2006-12-09 16:11 UTC |
| From | seta_t at mail dot goo dot ne dot jp |
| Assigned | alan_k |
| Status | Closed |
| Package | HTML_Template_Flexy |
| PHP Version | 5.1.6 |
| OS | Linux/Fedora Core 5 |
| Roadmaps | (Not assigned) |
Comments
[2006-12-09 16:11 UTC] seta_t at mail dot goo dot ne dot jp
Description:
------------
I want radiobuttons not to require IDs, and setValue to become able to be called with name of radiobuttons.
Expected result:
----------------
I want to
a template like:
<html>
<body>
<form>
<input type="radio" name="radio" value="1"/> 1
<input type="radio" name="radio" value="2"/> 2
</form>
</body>
</html>
and PHP code like:
...
$elements = $template->getElements();
$elements['radio']->setValue('2');
$template->outputObject($data,$elements);
...
output HTML like:
<html>
<body>
<form>
<input type="radio" name="radio" value="1" /> 1
<input type="radio" name="radio" value="2" checked /> 2
</body>
</html>
The following patch is written to do that.
diff -ru Flexy.orig/Element.php Flexy/Element.php
--- Flexy.orig/Element.php 2006-12-08 23:48:38.000000000 +0900
+++ Flexy/Element.php 2006-12-09 23:53:37.000000000 +0900
@@ -387,6 +387,11 @@
HTML_Template_Flexy_Element_Xul::setValue($this,$value);
return ;
+ case '_radiogroup':
+ foreach($this->children as $i=>$child){
+ $child->setValue($value);
+ }
+ return;
default:
if (is_array($value)) {
return;
--- Flexy.php.orig 2006-12-09 22:35:11.000000000 +0900
+++ Flexy.php 2006-12-10 00:04:44.000000000 +0900
@@ -146,6 +146,7 @@
// eg. = array('Savant') - loads the Savant methods.
// = array('MyClass_Plugins' => 'MyClass/Plugins.php')
// Class, and where to include it from..
+ 'setRadioValueByName' => false, // allows to call setValue for radiobuttons by name, and allows radiobuttons not to have IDs
);
/**
* The compiled template filename (Full path)
diff -ru Flexy.orig/Compiler/Flexy/Tag.php Flexy/Compiler/Flexy/Tag.php
--- Flexy.orig/Compiler/Flexy/Tag.php 2006-12-08 23:48:38.000000000 +0900
+++ Flexy/Compiler/Flexy/Tag.php 2006-12-10 00:54:27.000000000 +0900
@@ -634,6 +634,19 @@
$mergeWithName = true;
}
+ if ($this->compiler->options['setRadioValueByName'] &&
+ (strtolower($this->element->getAttribute('TYPE')) == 'radio' )) {
+ if ($this->element->getAttribute('ID') === false) {
+ $id = 'tmpId'. (++$tmpId);
+ // do not set ID to the element in order not to show tmpId in the output html
+ //$this->element->attributes['id'] = $id;
+ //$this->element->ucAttributes['ID'] = $id; // Actually, this one is not important for not showing tmpId
+ } else {
+ $id = $this->element->getAttribute('ID');
+ }
+ // in this case, $_HTML_TEMPLATE_FLEXY['elements'][$name] becomes not false, and be output if $mergeWithName == true
+ $mergeWithName = false;
+ }
@@ -740,6 +753,11 @@
// this is for a case where you can use a sprintf as the name, and overlay it with a variable element..
$_HTML_TEMPLATE_FLEXY['elements'][$id] = $this->toElement($this->element);
+ if($this->compiler->options['setRadioValueByName'] &&
+ (strtolower($this->element->getAttribute('TYPE')) == 'radio' )) {
+ // set the radio element as a "child" of the "parent" _radiogroup element
+ $_HTML_TEMPLATE_FLEXY['elements'][strtolower($this->element->getAttribute('NAME'))]->children[] = & $_HTML_TEMPLATE_FLEXY['elements'][$id];
+ }
if ($varsOnly) { // used by form tag.
@@ -866,27 +884,48 @@
}
// checkboxes need more work.. - at the momemnt assume one with the same value...
if (in_array(strtoupper($this->element->getAttribute('TYPE')), array('RADIO'))) {
-
- if (!isset($_HTML_TEMPLATE_FLEXY['elements'][$id])) {
- // register it.. - so we dont overwrite it...
- $_HTML_TEMPLATE_FLEXY['elements'][$id] = false;
- } else if ($_HTML_TEMPLATE_FLEXY['elements'][$id] != false) {
-
-
- return HTML_Template_Flexy::raiseError(
- "Error:{$_HTML_TEMPLATE_FLEXY['filename']} on Line {$this->element->line} ".
- "in Tag <{$this->element->tag}>:<BR>".
- "The Dynamic tag Name '$id' has already been used previously by ".
- "tag <{$_HTML_TEMPLATE_FLEXY['elements'][$id]->tag}>",
- null, HTML_TEMPLATE_FLEXY_ERROR_DIE
- );
+ if ((strtoupper($this->element->getAttribute('TYPE')) == 'RADIO') &&
+ $this->compiler->options['setRadioValueByName']){
+ if (!isset($_HTML_TEMPLATE_FLEXY['elements'][$id])) {
+ $_HTML_TEMPLATE_FLEXY['elements'][$id] = new HTML_Template_Flexy_Element('_radiogroup');
+ } else if ($_HTML_TEMPLATE_FLEXY['elements'][$id]->tag != '_radiogroup'){
+
+
+ return HTML_Template_Flexy::raiseError(
+ "Error:{$_HTML_TEMPLATE_FLEXY['filename']} on Line {$this->element->line} ".
+ "in Tag <{$this->element->tag}>:<BR>".
+ "The Dynamic tag Name '$id' has already been used previously by ".
+ "tag <{$_HTML_TEMPLATE_FLEXY['elements'][$id]->tag}>",
+ null, HTML_TEMPLATE_FLEXY_ERROR_DIE
+ );
+ }
+ } else {
+ if (!isset($_HTML_TEMPLATE_FLEXY['elements'][$id])) {
+ // register it.. - so we dont overwrite it...
+ $_HTML_TEMPLATE_FLEXY['elements'][$id] = false;
+ } else if ($_HTML_TEMPLATE_FLEXY['elements'][$id] != false) {
+
+
+ return HTML_Template_Flexy::raiseError(
+ "Error:{$_HTML_TEMPLATE_FLEXY['filename']} on Line {$this->element->line} ".
+ "in Tag <{$this->element->tag}>:<BR>".
+ "The Dynamic tag Name '$id' has already been used previously by ".
+ "tag <{$_HTML_TEMPLATE_FLEXY['elements'][$id]->tag}>",
+ null, HTML_TEMPLATE_FLEXY_ERROR_DIE
+ );
+ }
}
$id = $this->element->getAttribute('ID');
if (!$id) {
- return HTML_Template_Flexy::raiseError("Error on Line {$this->element->line} <{$this->element->tag}>:
- Radio Input's require an ID attribute (eg <input type='radio' id='1' name='xxxx' value='yyy'>..",
- null, HTML_TEMPLATE_FLEXY_ERROR_DIE);
+ if ((strtoupper($this->element->getAttribute('TYPE')) == 'RADIO') &&
+ $this->compiler->options['setRadioValueByName']){
+ $id = 'dummy';
+ } else {
+ return HTML_Template_Flexy::raiseError("Error on Line {$this->element->line} <{$this->element->tag}>:
+ Radio Input's require an ID attribute (eg <input type='radio' id='1' name='xxxx' value='yyy'>..",
+ null, HTML_TEMPLATE_FLEXY_ERROR_DIE);
+ }
}
$mergeWithName = true;
diff -ru Flexy.orig/Element.php Flexy/Element.php
--- Flexy.orig/Element.php 2006-12-08 23:48:38.000000000 +0900
+++ Flexy/Element.php 2006-12-09 23:53:37.000000000 +0900
@@ -387,6 +387,11 @@
HTML_Template_Flexy_Element_Xul::setValue($this,$value);
return ;
+ case '_radiogroup':
+ foreach($this->children as $i=>$child){
+ $child->setValue($value);
+ }
+ return;
default:
if (is_array($value)) {
return;
[2006-12-31 21:14 UTC] mail at paul dot kishimoto dot name
I don't have any comments on the proposed patch, but the requirement that radio <input>s have an ID is troublesome when using the <label> element.
I tried to use:
<label flexy:foreach="fruit,id,name" for="fruit-{id}"><input id="fruit-{id}" name="fruit" type="radio" value="{id}" /> {name} </label>
to produce code like:
<label for="fruit-1"><input id="fruit-1" name="fruit" type="radio" value="1" /> Apple</label>
<label for="fruit-5"><input id="fruit-5" name="fruit" type="radio" value="5" /> Orange</label>
<label for="fruit-12"><input id="fruit-12" name="fruit" type="radio" value="12" /> Banana</label>
from an array:
$this->fruit = array(
1 => 'Apple',
5 => 'Orange',
12 => 'Banana'
);
... but was told the input element must have an ID. I assume this means that the {id} variable in the <input id=> attribute cannot be parsed by Flexy and is being ignored. Flexy doesn't complain about the value attribute when I replace id="fruit-{id}" with id="fruit-x", which generates three elements with the same ID (ie. invalid XHTML) but different values.
In my expected output, clicking on the label text causes the corresponding radio input to be selected. This is good form design and common in XHTML. The flexy:nameuses attribute isn't helpful here, because it causes the radio inputs to have different names, breaking their behaviour.
Aside from generating HTML in my PHP code, I can't figure out a way to dynamically generate radio buttons with labels. Perhaps relaxing the id requirement, parsing the {id} variable or some other API change would allow this sort of output.