Title: Magento Adminhtml - How to Create New Admin Form
As a senior developer and technical blogger for Laravel Company, I’ll guide you through creating a new admin form in Magento, specifically focusing on the provided extension.
Current Setup Analysis
You’ve already set up an adminhtml page with a grid table that fetches data from a custom MySQL table called VivasIndustries_SmsNotification. Your current files and setup include:
-
Config XML File: The
config.xmlfile is correctly configured to define the module, models, resources, events, helpers, blocks, and adminhtml routers. -
Block Files: You have two block files -
VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status.phpfor the container andVivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Grid.phpfor the grid. These are properly extended from Magento’s base classes. -
Controller File: The
Adminhtml_SmsorderstatusesController.phpfile is set up with basic actions likeindexAction,gridAction,newAction, andeditAction. However, thenewActionandeditActionmethods are not fully implemented yet.
Creating a New Admin Form
To create a new admin form:
Step 1: Create an Edit Form Block
Create a new file under /app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status/Edit.php:
class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
$this->_blockGroup = 'smsnotification';
$this->_controller = 'adminhtml_sms_status';
$this->_headerText = Mage::helper('smsnotification')->__('Create New SMS Rule');
$this->_updateButton('save', 'label', Mage::helper('smsnotification')->__('Save SMS Rule'));
}
protected function _prepareLayout()
{
$this->setChild(
'form',
$this->getLayout()->createBlock('smsnotification/adminhtml_sms_status_edit_form')
);
}
}
Step 2: Create the Edit Form
Create a new file under /app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status/Edit/Form.php:
class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form(
array('id' => 'edit_form', 'action' => $this->getUrl('*/*/save'), 'method' => 'post')
);
$fieldset = $form->addFieldset('edit_fieldset', array('legend'=>Mage::helper('smsnotification')->__('SMS Rule Information')));
$fieldset->addField('Receiver', 'text', array(
'label' => Mage::helper('smsnotification')->__('Receiver'),
'required' => true,
'name' => ' Receiver'
));
$fieldset->addField('Phone', 'text', array(
'label' => Mage::helper('smsnotification')->__('Phone'),
'required' => true,
'name' => ' Phone'
));
$fieldset->addField('Date', 'text', array(
'label' => Mage::helper('smsnotification')->__('Date'),
'required' => true,
'name' => ' Date'
));
return parent::_prepareForm();
}
}
Step 3: Update the newAction and saveAction
Update your Adminhtml_SmsorderstatusesController.php file:
// New action to create a new SMS rule form
public function newAction()
{
$this->loadLayout()->_setActiveMenu('vivassms')
->renderLayout();
}
// Save action to save the new SMS rule data
public function saveAction()
{
if ($data = $this->getRequest()->getPost()) {
try {
$model = Mage::getModel('smsnotification/smsnotification');
$model->setData($data);
// Save the data to your custom MySQL table
$model->save();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('smsnotification')->__('SMS Rule saved successfully!'));
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
// Redirect to the grid page
$this->_redirect('*/*/');
}
Conclusion
By following these steps, you should now have a functioning new admin form under the “Create new SMS Rule” button. When you click this button, it will display an edit form with three input fields (Receiver, Phone, Date). After entering data and hitting the “Save SMS Rule” button, the data will be saved into your custom MySQL table.
Remember to clear your Magento cache after making these changes. If you have any further questions or need more assistance, feel free to reach out on our support channels at Laravel Company.
Happy coding!