if DropDown has value with index 0 in valueList

73 views
Skip to first unread message

Liviu Ifrim

unread,
Mar 31, 2014, 9:48:05 AM3/31/14
to agile-too...@googlegroups.com
Hi,

I have an issue in this example:

$f = $this->add('Form');
$f
->addField('dropdown', 'mydropdown')
       
->setEmptyText('Please, select')
       
->setValueList(array('No', 'Yes))
;

As it can be seen my 'No' value has index 0, and on the page the selected value will be No even if I didn't set a value.
The problem, i think is in the Form_Field_DropDown class in getOption method where the comparison is made with == so 0 and empty string which is the index of the empty text will be both seen as null, two options will have selected attribute and in the dropdown the selected value in this case will be 'No' because it is the last option which has selected.

In my opinion a fix could be:

function getOption($value){
        $selected
= false;
       
if($this->value===null){
            $selected
= $value==='';
       
} else {
            $selected
= $value == $this->value;
       
}
       
return $this->getTag('option',array(
                   
'value'=>$value,
                   
'selected'=>$selected
                   
));
   
}




Miss Khushbu

unread,
Apr 2, 2014, 4:25:12 AM4/2/14
to agile-too...@googlegroups.com
Try using 

$f = $this->add('Form');
$f
->addField('dropdown', 'mydropdown')
       
->setEmptyText('Please, select')

       
->setValueList(array('N'=>'No','Y'=> 'Yes))  // <=== Check Here

dropdown sends key when form is submitted and No is on zero index.. 0 also represents false :)

Liviu

unread,
Apr 2, 2014, 5:39:36 AM4/2/14
to agile-too...@googlegroups.com
Thanks for the reply,

I know that the key is sent when form is submitted, I just wanted to bring out this issue, my problem is that I don't want to change the keys on my array so even if I have array(0=>false, 1=>true) I want to use this indexes.

I think this is a bug in atk4, the selected attribute will be added to both options (the option with the key 0 and the empty text option)  and it shouldn't, until now this wasn't an issue because the empty text was added at the end of the dropdown list, and the dropdown had as selected the empty text value (which was the last selected option).
 
I have extended the dropdown class from atk and made my own class where I have made the changes from my first post, so in my project all works well now. Just wanted to bring out this issue and see if I am right and this is a bug or I didn't understood well the problem and the issue is in another place.

Thanks,
Liviu

Romans Malinovskis

unread,
Jul 20, 2014, 3:35:15 PM7/20/14
to agile-too...@googlegroups.com
Hi Liviu

Would you be able to share a gist or a pull request with your changes?

Thanks


romans

Liviu

unread,
Jul 21, 2014, 4:45:23 AM7/21/14
to agile-too...@googlegroups.com
Hi Romans,
I made a pull request with the code from the first post.

Romans Malinovskis

unread,
Jul 21, 2014, 5:54:46 AM7/21/14
to agile-too...@googlegroups.com
oh great i see it now.

Imants Horsts

unread,
Aug 19, 2014, 1:10:59 PM8/19/14
to agile-too...@googlegroups.com
This was almost correct, but still need a bit of improvement.

Romans Malinovskis

unread,
Aug 22, 2014, 10:09:40 AM8/22/14
to agile-too...@googlegroups.com
merged
Reply all
Reply to author
Forward
0 new messages