Daily Archives: August 23, 2009

Zend Framework

Okay, this little bit of  code is my attempt to give back to the Open Source community.

While there are some things I really love about the Zend_Framework.  Their documentation could be much better.  They really do a poor job of giving examples of how the options effect a given piece of code.  CakePHP does a much, much better job of showing how the options effect the outcome with examples.  One very bizarre thing I’ve noticed are people posting the exact same sample code over and over again as if it was their own.  None of it highlighting anything new.  What’s up with that?

Okay, enough whining.  If google finds this I hope it helps you.  It took me long enough to track it down and like most things, it was simple, but just not documented very well.

If you create a set of Radio Buttons with the Zend_Form_Element_Radio() class, you just might want to set a default button checked.  Here’s how you do it.

$myradio = new Zend_Form_Element_Radio(‘foo’);
$myradio->setLabel(‘Foo:’)
->addMultiOptions(array(‘v1′=>’Bar’,
‘v2′=>’Boo’,
‘v3′=>’baz’))
->setAttrib(‘class’,'coolcss’)
->setValue(‘v2′);

When you render this form in your view (maybe with a form->foo ?> call. All your options will be rendered and the radio button ‘Boo’ will be checked. That’s it. It’s that simple.

It’s not exactly obvious, but it’s simple. Why not obvious? Because check boxes in HTML are set with the attributed checked=”checked”. Setting a radio button value sets the value of the input – i.e. the value returned TO the server. It’s not necessarily obvious that setting the value of a radio controller makes it “checked”. Shame Zend didn’t stay consistent with HTML. But there it is and it works.

Happy Coding.
– Sno