Challenge 4.4: Controller Forms

On the last challenge we refactored the code of our controller so the edit and new actions could use a shared save action. Now that we have made that change we need to change both the layout and the templates to use a unique form, that will handle both actions.

Since both the edit and new actions are using the same save action there is no need to have 2 different sets of templates so let's edit our layout file:

In the layout file we are referencing a new template file called form.phtml let's create the template file:

The new form template code calls the following code:

$loadedRegistry = Mage::registry('current_giftregistry');

Which loads the model that was saved inside the Magento registry(not to be confused with the gift registry), the registry is one of the coolest features of Magento, think about the registry as a manager for global variables; that we can use to easily access information across different parts of the application.

The template also tries to pre-populate the field values with the registry data, this will only work when we are editing an existing registry, otherwise all the fields should be empty.

Conclusion

Magento layout and template system gives a lot of flexibility and power that we can use to build complex and useful forms and views.