Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phalcon\Forms\Element\Select::addOption(array $option) #1955

Closed
alvinkohcm opened this issue Feb 1, 2014 · 3 comments
Closed

Phalcon\Forms\Element\Select::addOption(array $option) #1955

alvinkohcm opened this issue Feb 1, 2014 · 3 comments

Comments

@alvinkohcm
Copy link

Class: Phalcon\Forms\Element\Select

Version: : Phalcon 1.2.6
Issue: Method addOption() does not correctly render argument type Array as select option.

addOption(array $option)
$selection = new Phalcon\Forms\Element\Select("selection");
$selection->addOption(array('id'=>'1','value'=>'Apple')); //Does not work
$selection->addOption(array('2'=>'Orange')); //Does not work
$selection->addOption('Test String'); //Works with String
echo $selection->render(array('multiple'=>'yes'));

$selection2 = new Phalcon\Forms\Element\Select("selection2"); 
$selection2->setOptions(array('1'=>'Apple','2'=>'Orange','3'=>'Test String'));
echo $selection2->render(array('multiple'=>'yes'));

Output:

<select multiple="yes" name="selection" id="selection">
    <option value="0">Array</option>
    <option value="1">Array</option>
    <option value="2">Test String</option>
</select>

<select multiple="yes" name="selection2" id="selection2">
    <option value="1">Apple</option>
    <option value="2">Orange</option>
    <option value="3">Test String</option>
</select>

select_addoption_output

@ghost
Copy link

ghost commented Feb 2, 2014

Fixed, could you please check?

@alvinkohcm
Copy link
Author

Much thanks @sjinks for responding to this issue.
Checked (v1.3.0) and result does not seem to be ideal:

<select id="selection" name="selection" multiple="yes" size="10">
   <optgroup label="0">
      <option value="id">1</option>
      <option value="value">Apple</option>
   </optgroup>
   <optgroup label="1">
      <option value="2">Orange</option>
   </optgroup>
   <option value="2">Test String</option>
</select>
  • $option added via addOption() is contained in another array resulting in the <option>(s) being encased in <optgroup> group tags
  • From a user perspective, array('value'=>'label') seems to be the expected format required e.g. array('2'=>'Orange') as a HTML <option> always contains a value and a corresponding display label.

phalcon_select_output2

$selection = new \Phalcon\Forms\Element\Select("selection");
$selection->addOption(array('id'=>'1','value'=>'Apple'));
$selection->addOption(array('2'=>'Orange'));
$selection->addOption('Test String');

echo $selection->render(array('multiple'=>'yes','size'=>10)); // render Select element

Output from: print_r($selection->getOptions());

Array
(
    [0] => Array
        (
            [id] => 1
            [value] => Apple
        )
    [1] => Array
        (
            [2] => Orange
        )
    [2] => Test String
)

@ghost
Copy link

ghost commented Feb 3, 2014

Please run

php build/gen-build.php

then rebuild Phalcon and try again — build/ version is not updated automatically from ext/

@alvinkohcm
Copy link
Author

<select id="selection" name="selection" multiple="yes" size="10">
    <option selected="selected" value="0">Test String</option>
    <option value="2">Orange</option>
    <option value="l">1</option>
    <option value="value">Apple</option>
</select>

Works after building from ext/
Thank you!

@ehsanmr
Copy link

ehsanmr commented May 25, 2014

i have same issue with optgroup
should i edit ext/tag/select.c file and recompile or there is a better solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants