Home » HTML » HTML_Template_Flexy » Bug #4687
Dynamic generated form elements are ignored
Details
| Submitted | 2005-06-26 18:48 UTC |
|---|---|
| From | jo at feuersee dot de |
| Status | Bogus |
| Package | HTML_Template_Flexy |
| PHP Version | 5.0.4 |
| OS | Linux |
| Roadmaps | (Not assigned) |
Comments
[2005-06-26 18:48 UTC] jo at feuersee dot de
Description:
------------
This is maybe related/similar to bug #204
(http://pear.php.net/bugs/bug.php?id=204), however, the
proposed solution there didn't help.
I am using (the BTW superb) Flexy Template to generate a
HTML document from session data to implement a shopping
basket. Because of the nature of the circumstances, the
names of the form data have to be dynamically generated.
I am using PHP style configuration files to store the
attributes of form elements, eg.:
$conf = array(
'shipping' => array(
'attributes' => array(
'type' => 'text',
'size' => 8,
'maxlength' => 8,
'readonly' => 'readonly'
),
'setValue' => sprintf('%.2f', $this->country[0]
['Shipping'])
),
...
);
--eof
This data is processed and passed to the appropriate
HTML_Template_Flexy_Element method, which works perfectly
for statically named form elements.
For the dynamic named form elements I tried to add
elements like this:
// SQL query to get results into $this->summary
$i = 0;
foreach($this->summary as $key => $val) {
$conf["items$i"] = array(
'attributes' => array(
'type' => 'text',
),
'setValue' => $this->summary['items']
);
}
--eof
However, the results aren't as expected (as seen below).
Any way to work artound this and still keep the template
approach?
Reproduce code:
---------------
template:
{foreach:summary,key,row}
<input name="price{key}" butt="head">
<input name="items%s" flexy:nameuses="member.line" butt="head">
{end:}
--eof
Expected result:
----------------
<input name="price0" type="text" value="22.00">
<input name="items0" type="text" value="1">
<input name="price1" type="text" value="33.00">
<input name="items1" type="text" value="2">
...
--eof
Actual result:
--------------
<input name="price0" butt="head">
<input name="items" butt="head">
<input name="price1" butt="head">
<input name="items" butt="head">
[2005-06-26 18:51 UTC] jo at feuersee dot de
Uhm, forgot the $i++; inside the foreach()...
[2005-06-27 09:40 UTC] jo at feuersee dot de
Nope, it still doesn't work.
But the $i++ is obviously necessary in order for the
foreach loop to create a proper data structure, so I
posted it to prevent "you need a $i++ inside your foreach"
replies.
I double checked both the results of the SQL query and the
generated data, they are ok and not the problem here.
[2005-06-28 19:08 UTC] jo at feuersee dot de
test-4683.php:
<?php
$options = array(
'templateDir' => dirname(__FILE__),
'compileDir' => dirname(__FILE__) .
DIRECTORY_SEPARATOR . 'tmp',
'forceCompile' => 0,
'debug' => 0,
'compiler' => 'Flexy',
);
require_once('HTML/Template/Flexy.php');
$flexy = new HTML_Template_Flexy($options);
$flexy->compile('template-test-4683.html');
require_once('HTML/Template/Flexy/Element.php');
$obj = new StdClass;
// static form element
$elem['formtest'] = &new HTML_Template_Flexy_Element();
$elem['formtest']->attributes['method'] = 'post';
$elem['formtest']->attributes['action'] =
$_SERVER['PHP_SELF'];
$obj->data = array();
for($i = 0; $i < 10; $i++) {
$obj->data[$i] = $i;
$elem["data$i"] = &new
HTML_Template_Flexy_Element();
$elem["data$i"]->attributes['type'] = 'text';
$elem["data$i"]->attributes['size'] = $i;
}
$flexy->outputObject($obj, $elem);
?>
--eof
template-test-4683.php:
<html>
<head>
<title>test for PEAR bug#4683</title>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
</head>
<body>
<form name="formtest">
{foreach:data,key,row}
{key}: <input name="data{key}" type="text"
size="10"><br>
{end:}
</form>
</body>
</html>
--eof
The result:
<html>
<head>
<title>test for PEAR bug#4683</title>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
</head>
<body>
<form name="formtest" method="post"
action="/~jo/test/test-4683.php"> 0: <input
name="data0" type="text" size="10"><br>
1: <input name="data1" type="text" size="10"><br>
2: <input name="data2" type="text" size="10"><br>
3: <input name="data3" type="text" size="10"><br>
4: <input name="data4" type="text" size="10"><br>
5: <input name="data5" type="text" size="10"><br>
6: <input name="data6" type="text" size="10"><br>
7: <input name="data7" type="text" size="10"><br>
8: <input name="data8" type="text" size="10"><br>
9: <input name="data9" type="text" size="10"><br>
</form>
</body>
</html>
--eof
I expected:
<html>
<head>
<title>test for PEAR bug#4683</title>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
</head>
<body>
<form name="formtest" method="post"
action="/~jo/test/test-4683.php"> 0: <input
name="data0" type="text" size="0"><br>
1: <input name="data1" type="text" size="1"><br>
2: <input name="data9" type="text" size="2"><br>
2: <input name="data2" type="text" size="3"><br>
3: <input name="data3" type="text" size="4"><br>
4: <input name="data4" type="text" size="5"><br>
5: <input name="data5" type="text" size="6"><br>
6: <input name="data6" type="text" size="7"><br>
7: <input name="data7" type="text" size="8"><br>
8: <input name="data8" type="text" size="9"><br>
</form>
</body>
</html>
[2005-06-29 11:20 UTC] jo at feuersee dot de
Ok, it does work now. I made a wrong use of the
undocumented flexy:nameuses="name" feature.
It does work in the original project except in one place,
so I guess I still have something wrong there.
THX for your help.
Bug closed, the call for better documentation can be
heard...