From: Cedrick Gout on
For some reason when I make a group with a select field in it and want to
set the default value for a value from 10-19 it set's it to one, 20-21 sets
to two etc.
Its as if only the first character get passed trough. (I am using the latest
htmlcommon and quickform2)

Some example code:

require_once 'HTML/QuickForm2.php';

$form = new HTML_QuickForm2('testSelect');
$form->addDatasource(new HTML_QuickForm2_DataSource_Array(array(
'test' => "16"
)));

$testSelect = $form->addElement('select', 'test', null, array('options' =>
$testSelectArray));

echo $form; // This works as expected, the select with value 16 gets
selected

Now we make a group

require_once 'HTML/QuickForm2.php';

$form = new HTML_QuickForm2('testSelect');
$form->addDatasource(new HTML_QuickForm2_DataSource_Array(array(
'testGroup' => "16" // values 1-9 are selected correctly
)));

$myTestGroup = $form->addElement('group', 'testGroup')->setLabel('Strange
select problem');
$myTestGroup->addElement('select', 'test', null, array('options' =>
$testSelectArray));

echo $form;

OUTPUT:

<select name="testGroup[test]" id="test-0">
<option value="0">Select something</option>
<option value="1" selected="selected">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>

<option value="5">E</option>
<option value="6">F</option>
<option value="17">G</option>
<option value="7">H</option>
<option value="8">I</option>
<option value="9">J</option>

<option value="10">K</option>
<option value="11">L</option>
<option value="12">M</option>
<option value="13">N</option>
<option value="15">O</option>
<option value="16">P</option>

</select>


Does this happen because I'm an idiot or is this a bug?

Best regards,
Cedrick
From: Alexey Borzov on
Hi Cedrick,

On 07.10.2010 0:28, Cedrick Gout wrote:
> For some reason when I make a group with a select field in it and want to
> set the default value for a value from 10-19 it set's it to one, 20-21 sets
> to two etc.
>
> Now we make a group
>
> require_once 'HTML/QuickForm2.php';
>
> $form = new HTML_QuickForm2('testSelect');
> $form->addDatasource(new HTML_QuickForm2_DataSource_Array(array(
> 'testGroup' => "16" // values 1-9 are selected correctly
> )));

First of all, you are passing a wrong value for a group here, should be
'testGroup' => array('test' => 16)


> OUTPUT:
>
> <select name="testGroup[test]" id="test-0">
> <option value="1" selected="selected">A</option>
....
> </select>
>
>
> Does this happen because I'm an idiot or is this a bug?

Looks like PHP has pretty strange behaviour concerning string offsets, the
following code:
$testGroup = "13";
var_dump(isset($testGroup['test']), $testGroup['test']);

outputs (without any error messages):
bool(true)
string(1) "1"

It just silently converts 'test' to integer 0 and returns first symbol of the
string.

So when select element asks Array DataSource for a value of element named
'testGroup[test]' it gets the first symbol of a value for an element named
'testGroup' instead of a more obvious null.

Thanks for the report, I think adding an extra is_array() check to Array
DataSource may make its behaviour a bit more predictable.
 | 
Pages: 1
Prev: Issue saving the dateelement