Friday, July 22, 2011

[ZF] Marking navigation tab as active with GET data in request

So I have a contract that I want to edit for a customer. I happily click on the "EDIT" link and the form appears, but wait a minute, something's wrong! The "contract" tab in my menu isn't showing up as the active tab anymore.

The main contract page showed the active class with this in the navigation XML file:
<contract>
    <label>Contracts</label>
    <uri>/contract/</uri>
    <resource>contract</resource>
    <privilege>index</privilege>
</contract>


...and to view a contract, I simply use (as a page of the "contract" section):
<contract-view>
    <label>View contract</label>
    <uri>/contract/view/</uri>
    <visible>0</visible>
</contract-view>


But if I want to view a contract, the ID goes through the url, as such: http://www.example.com/contract/view/123/

And when this happens, the navigation file doesn't detect that the current page is the one I want. To counter this, all you have to do is use the alternate method, which is a little longer but prevents any problem:
<contract-view>
    <label>View contract</label>
    <controller>contract</controller>
    <action>view</action>
    <visible>0</visible>
</contract-view>


When you specify the action and controller directly, the framework will take care of the rest, and even if you're using routes, everything will display smoothly. This is what I've set up for an URL like http://www.example.com/contract/123/view/
- in the navigation file:
<contract-view>
    <label>View contract</label>
    <controller>contract</controller>
    <action>view</action>
    <visible>0</visible>
</contract-view>

- in the routes.ini file:
routes.contractview.route = contract/:id/view
routes.contractview.defaults.controller = contract
routes.contractview.defaults.action = view
routes.contractview.defaults.id = 0


And there you have it! The Zend navigation and router will take care of the rest for you. Now it will detect the page you're on regardless of the URL, as long as the correct controller and action are defined in the configuration files. The beautiful tab is glowing as it should!

Monday, July 18, 2011

[ZF] Merging multiple Zend_Form into one

So you want to merge two or more of your Zend forms into one, right?

This is really easy to do, actually...

First of all, get a blank form:
$form = new Application_Form_UserInformation();

Then, get the other form(s):
$form_append = new Application_Form_UserAddress();

All you have to do is take all the elements from the other form(s) and add them to the first one!
$form->addElements($form_append->getElements());

You can still remove elements or add any by using addElement() or removeElement(). However, watch out for element names! If elements from two separate forms have the same one, the first you set will overwrite the others. This is useful for submit buttons, for example.

Be sure to always have your submission button at the end of your form, before sending it to your view:
$form->submit->setOrder(999);
$this->view->form = $form;


Piece of cake, told you so!