Hi,
i want to use a "multiple" select (a select box where you can check several options), so i create a Phalcon\Forms\Select element giving it the "multiple" = true option. The render() method generates the HTML without putting "[]" (opening and closing brackets) after the tag name. This causes PHP to keep only one value in the _POST global variable.
The obvious "solution" (to me) would be to name the select with ending brackets. This way PHP populates _POST with all the selected values but under a key without the brackets (as always) and then the Phalcon validator fails because it looks for a value with brackets (which doesn't exists) !
This issue seems so basic that i feel i must be missing something. As i was not able to find an answer in the documentation or forum i ask it here.
Here is a sample code :
<form method="post">
<?php
$di = new \Phalcon\DI\FactoryDefault(); $form = new Phalcon\Forms\Form();
$selection = new Phalcon\Forms\Element\Select('multsel', array('1'=>'Apple','2'=>'Orange','3'=>'Test String'), array('multiple'=>'yes'));
// $selection = new Phalcon\Forms\Element\Select('multsel[]', array('1'=>'Apple','2'=>'Orange','3'=>'Test String'), array('multiple'=>'yes'));
$selection->addValidators(array(new Phalcon\Validation\Validator\PresenceOf()));
$form->add($selection);
if (count($_POST) > 0) { echo '<pre>'; var_dump($form->isValid($_POST)); var_dump($_POST); echo '</pre>'; }
echo $selection->render('testmultisel'); ?> <input type="submit" name="sbm" value="Submit" /> </form> </pre>
Depending on which line ($selection...) i uncomment i have different behaviour but both are incorrect :
- with the select named "multsel" i only have 1 value in $_POST
- with the select named "multsel[]", $form->isValid($_POST) returns false
This issue happens also with checkboxes that are part of the same group : there seem to be no easy way to use them in Phalcon.
Tested using Phalcon 1.3.x