ZF-6712: When using Zend_Form_Element_Multi (radio) - id is generated from value. This causes issues with negative to positive ranges.


It appeas that labels associated with individual radio button items have an id that is generated from the radio-set name (rating) and the value of each individual radio button. In the example below, you will see the values range from negative to positive values. Because of the way the id is generated, we will end up with duplicate elements sharing the same id. This not only breaks validation, but also usability, as clicking on a label no longer selects the expected radio button, as it should do.

There are ways around this from a developers perspective, Bittarman in IRC suggested adding a fixed number to each of the values and subtracting them after form submission. Personally, I find that a dirty hack that should be avoided. I could also choose different values for my radios, but again, the values here match what is expected in the application and the database, writing some means of converting them before saving them seems wrong too.

Perhaps, if we don't change the way the id's are generated by default, we could have an array which keeps a list of the id's already generated for a form and if a new id is generated that matches an existing id, we could append the id, perhaps starting up the alphabet.

[edit] In fact, because the label's encompass the radio form element, we do not even need the for attribute. Labels automatically point to the encompassed element when selected.

[one more edit!] I have also just noticed that the main label, set when doing {{$rating->setLabel('Rating')}} has a for attribute which has a non-existing id set. This also causes validation to fail, so preferably, there should be no for attribute here.

$rating = new Zend_Form_Element_Radio('rating');
    -2    =>    'very bad',
    -1    =>    'bad',
     0    =>    'indifferent',
     1    =>    'good',
     2    =>    'very good'

Regards, Steven


This is also related to this issue: http://framework.zend.com/issues/browse/ZF-4191

You will see in this bug report it's to do with the filter applied to option values when creating the ids/.

I have unfortunately come across this little issue this morning although it appears to have been an issue since the radio elements release.