More on the ongoing struggle with the Zend Framework’s documentation….
After much trawling of the Zend Framework Manual, ZF on Nabble, and Google, I finally tracked down how to include & manipulate radio buttons using Zend_Form (ZF 1.5).
The HTML code I was trying to output was something along the lines of:
<input type="radio" value="1" name="staff" /> <label>Jim</label> <br/>
<input type="radio" value="2" name="staff" /> <label>John</label>
Apparently, to do this the setMultiOptions needs to be used, although that this is required is not made clear in the Zend_Form Documentation. To do it:
Zend_Loader::loadClass('Zend_Form');
Zend_Loader::loadClass('Zend_Form_Element_Radio');
$form = new Zend_Form;
$staffname = array('Jim', 'John');
$staff = new Zend_Form_Element_Radio('staff');
$staff->setRequired(true) // field required
->setValue('R') // first radio button selected
->setMultiOptions($staffname); // add array of values / labels for radio group
$form->addElement($staff);
However, I also needed to specify the value attribute of each of the radio buttons. To set this value, the key value of the array being passed into setMultiOptions is used. So, for example,
$options = StaffTable::getStaff(); // get staff details from StaffTable
$staffname = array();
foreach($options as $st){
$staffname[$st['staff_id']] = $st['staff_name']; // set staff_id as the key
}
This is retrieving a recordset of staff id’s and staff names for use in the radio button group. It uses the value of staff_id for the key value of the $staffname array. This will then be used as the value in the radio buttons. So if this $staffname array is passed into setMultiOptions, the output is: (note: the staff_id values for Jim and John were 32 and 28 respectively)
<label style="white-space: nowrap;"><input name="staff" type="radio" value="32" />Jim</label>
<label style="white-space: nowrap;"><input name="staff" type="radio" value="28" />John</label>
During all my searching, it seems that there’s an acknowledgement (but some frustration) that the documentation for certain parts of the framework (such as Zend_Form) is lacking in both detail and useful examples. That said, I’ve been finding it pretty useful despite the learning curve and the occasionally long bouts of searching the web to solve problems…
8 Comments
6:58 pm
Permalink
I’m happy to hear that things are working for you now. I’ll forward this to Matthew so he can address your concerns; if there are improvements to be made- documentation or otherwise- we should get them in to the issue tracker ASAP.
,Wil
8:13 am
Permalink
Hi Wil,
Thanks for the comment, and for passing it along.
Yes, you’re right of course, the issue tracker should have been my first stop…it will be next time
Dave.
8:58 am
Permalink
Thanks for info. I was struggling a lot for radio buttons.
Can you tell me how can we aling radio buttons horizontally.
9:41 am
Permalink
Hi Aniket,
Glad to hear it helped.
I haven’t tried horizontal alignment, but I’m sure that you could do that using CSS (and definitely using some JavaScript & CSS)
1:22 am
Permalink
Dave, I just have to say, your comments on the lack of documentation for the ZF are refreshing. If I have to hear one more time that “it’s easy” I’m going to pull my hair out. They’re basically asking php programmers to completely re-learn how to build php and html pages. OK, no problem, but how? They tell you the what, but not the where. For instance, they’ll say just instantiate, such and such…. fine, but where? what file and what directory? This tutorial illustrates just how incomplete their documentation really is. There’s nothing in their documentation on how to use an html radio buttons. And why does every incarnation of the framework, use a different directory structure? Everyone claims it’s great, but the learning curve is so huge that it’s only great if you have a month to sit down and recreate the wheel. Anyway, thanks for all your efforts!
4:20 am
Permalink
++ to the infuriating docs. They are annoyingly obtuse at times.
It was easier to use google and find this blog post to grab the correct syntax than to use the official docs. So thanks for taking the time to write it
11:02 am
Permalink
@Mark, @firecall
Thanks for the comments guys.
I can relate to the frustration, however, I’m wary of being overly harsh on problems with the docs, as I think it’s a bit unfair because it’s an open source project. When I started out with Zend, I found the learning curve incredibly frustrating (and made worse by problems I had understanding some of the docs); that is why I’ve posted some stuff on this blog.
I suppose the best way to address these problems is to do as Wil suggested above – contribute documentation back to the community; I also think that posting tutorials / fixes you’ve worked out on blogs can be really helpful for others feeling the frustration you’ve experienced.
7:31 am
Permalink
Hi, Dave
Many Thanks for this help…..