Home » Magento tutorial for developer » How to create “Edit” form in Magento admin backend

How to create “Edit” form in Magento admin backend

In this tutorial for developer, I will guide you how to create an Edit form in backend of Magento. In particular, you will learn how to use extension Tutorialmagento_Salestaff to add/edit staff information in backend.

Main purpose: Create edit form that allows admin to add more staff or edit info of existing staff

Step 1. Change action “new” to “edit”

By default, when click on button “Add new”, you will be redirected to url  http://yoursite.com/index.php/salestaffadmin/staff/new .

This means the function newAction()  in controller  app\code\local\tutorialmagento\Salestaff\controllers\Adminhtml\StaffController.php  was executed

Basically, this function just allows to use forward function to edit action in the same controller “Staff”. I will explain in more detail below

Step 3. Add url to grid row when editting

Why we need to add url to row in grid? In the previous tutorials, I guided you how to create grid to show in front end, but how to changes information of these records? We solve this by adding “url to edit page” to the records in grid.

Now, add  getRowUrl($row)  to  fileapp\code\local\tutorialmagento\Salestaff\Block\Adminhtml\Staff\Grid.php :

From now, when we click on a record, it will be redirected to link http://yoursite.com/salestaffadmin/staff/edit/id/$id.

In function getUrl we will pass 2 parameters:

+ 1st parameter: action

*/*/edit: would be equivalent to salestaffadmin/staff/edit because the current action is salestaffadmin/staff/index

+ 2nd parameter: parameters

array(‘id’=>$row->getId)

Step 4: Create “edit” action

We can create editAction()  in file: app\code\local\tutorialmagento\Salestaff\controllers\Adminhtml\StaffController.php

This function allows to retrieve staff we want to edit and view object’s info, enables store owner to edit info.

Step 5. Create form “container”

Form “container” includes some control buttons like “Save”, “save and edit”, “delete”. 

Form container này bao gồm một số button control như “Save”, “Save and Edit”, “Delete”. This form is inherited from class Mage_Adminhtml_Block_Widget_Form_Container .

create edit form in magento backend 1

File: app\code\local\Tutorialmagento\Salestaff\Block\Adminhtml\Staff\Edit.php

Step 6. Create tab “Staff Information”

Actually, in this tutorial, it’s not necessary to use tab in edit staff admin section, however, I would still include this part in this tutorial so that you can better understand and use it when needed.

Create file: app\code\local\tutorialmagento\Salestaff\Block\Adminhtml\Staff\Edit\Tabs.php

In this function we use the following command:

Block salestaff/adminhtml_staff_edit_tab_form is the block that contains main display form of page edit.

File : app\code\local\Tutorialmagento\Salestaff\Block\Adminhtml\Staff\Edit\Tab\Form.php

Step 7. Create form “edit”

create edit form in magento backend 2

File:  app\code\local\Tutorialmagento\Salestaff\Block\Adminhtml\Staff\Edit\Tab\Form.php:

Conclusion

We’ve just finished creating form that allows to add/edit salestaff data. This is, however, just a data display/edit form, to make it work we need specific actions, we will continue in the next part of the series. Feel free to let me know if you have any questions

About Brian Wilson

avatar
Brian is a freelance Magento developer with 5 years of experience with customizing Magento extensions, magento themes. He's interested in sharing Magento Tutorials to the community

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Scroll To Top