4

I'm using Magento 1.9.0.1 and i'm currently working over a custom extension.

I've created a custom Edit Form in the admin panel and here is a screenshot of it:

enter image description here

The Save Item button is just like dead. When i click on it just nothing happens, no errors even no page loading/reloading.... just nothing.

The Delete Item button is working fine and it is deleting what it must delete.

Here is the code that i think is responsible for my strugle:

I Here is my Edit.php:

<?php
class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Edit extends Mage_Adminhtml_Block_Widget_Form_Container

{
    public function __construct()
    {
        parent::__construct();

        $this->_objectId = 'id';
        $this->_blockGroup = 'smsnotification';
        $this->_controller = 'adminhtml_sms_status';

        $this->_updateButton('save', 'label', Mage::helper('smsnotification')->__('Save Item'));
        $this->_updateButton('delete', 'label', Mage::helper('smsnotification')->__('Delete Item'));
    }

    public function getHeaderText()
    {
        if( Mage::registry('smsnotification_data') && Mage::registry('smsnotification_data')->getId() ) {
            return Mage::helper('smsnotification')->__("Edit Item '%s'", $this->htmlEscape(Mage::registry('smsnotification_data')->getTitle()));
        } else {
            return Mage::helper('smsnotification')->__('Add Item');
        }
    }
}

You can check the file path by the class..

Here is what i have in my Form.php file:

<?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', array('id' => $this->getRequest()->getParam('id'))),
                                        'method' => 'post',
                                     ));
                $this->setForm($form);
                $fieldset = $form->addFieldset('smsnotification_form', array('legend'=>Mage::helper('smsnotification')->__('Item information')));

                $fieldset->addField('Receiver', 'text', array(
                    'label'     => Mage::helper('smsnotification')->__('Receiver'),
                    'class'     => 'required-entry',
                    'required'  => true,
                    'name'      => 'Receiver',
                     ));

                $fieldset->addField('Phone', 'text', array(
                    'label'     => Mage::helper('smsnotification')->__('Phone'),
                    'class'     => 'required-entry',
                    'required'  => true,
                    'name'      => 'Phone',
                    ));

                $fieldset->addField('Date', 'text', array(
                    'label'     => Mage::helper('smsnotification')->__('Date'),
                    'class'     => 'required-entry',
                    'required'  => true,
                    'name'      => 'Date',
                    ));

                if ( Mage::getSingleton('adminhtml/session')->getsmsnotificationData() )
                    {
                        $form->setValues(Mage::getSingleton('adminhtml/session')->getsmsnotificationData());
                        Mage::getSingleton('adminhtml/session')->setsmsnotificationData(null);
                    } elseif ( Mage::registry('smsnotification_data') ) {
                        $form->setValues(Mage::registry('smsnotification_data')->getData());
                    }
                    return parent::_prepareForm();
            }
    }

Here is what i have in my Status.php file:

<?php

class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status extends Mage_Adminhtml_Block_Widget_Grid_Container


{
    public function __construct()
    {
        $this->_controller = 'adminhtml_sms_status';
        $this->_blockGroup = 'smsnotification';
        $this->_headerText = Mage::helper('smsnotification')->__('Item Manager');
        $this->_addButtonLabel = Mage::helper('smsnotification')->__('Add Item');
        parent::__construct();
    }
}

Here is what i have in: SmsorderstatusesController.php:

<?php

class VivasIndustries_SmsNotification_Adminhtml_SmsorderstatusesController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->_title($this->__('SMS Center'))->_title($this->__('SMS Center'));
        $this->loadLayout();
        $this->_setActiveMenu('vivassms');
        $this->_addContent($this->getLayout()->createBlock('smsnotification/adminhtml_sms_status'));
        $this->renderLayout();
    }

    public function editAction()
    {
        $smsnotificationId     = $this->getRequest()->getParam('id');
        $smsnotificationModel  = Mage::getModel('smsnotification/smsnotification')->load($smsnotificationId);

        if ($smsnotificationModel->getId() || $smsnotificationId == 0) {

            Mage::register('smsnotification_data', $smsnotificationModel);

            $this->loadLayout();
            $this->_setActiveMenu('smsnotification/items');

            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager'));
            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));

            $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);

            $this->_addContent($this->getLayout()->createBlock('smsnotification/adminhtml_sms_status_edit'));

            $this->renderLayout();
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('smsnotification')->__('Item does not exist'));
            $this->_redirect('*/*/');
        }
    }

    public function newAction()
    {
        $this->_forward('edit');
    }

    public function saveAction()
    {
        if ( $this->getRequest()->getPost() ) {
            try {
                $postData = $this->getRequest()->getPost();
                $smsnotificationModel = Mage::getModel('smsnotification/smsnotification');

                $smsnotificationModel->setId($this->getRequest()->getParam('id'))
                    ->setReceiver($postData['Receiver'])
                    ->setPhone($postData['Phone'])
                    ->setDate($postData['Date'])
                    ->save();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setsmsnotificationData(false);

                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setsmsnotificationData($this->getRequest()->getPost());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        $this->_redirect('*/*/');
    }

    public function deleteAction()
    {
        if( $this->getRequest()->getParam('id') > 0 ) {
            try {
                $smsnotificationModel = Mage::getModel('smsnotification/smsnotification');

                $smsnotificationModel->setId($this->getRequest()->getParam('id'))
                    ->delete();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));
                $this->_redirect('*/*/');
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            }
        }
        $this->_redirect('*/*/');
    }
    /**
     * Product grid for AJAX request.
     * Sort and filter result for example.
     */
    public function gridAction()
    {
        $this->loadLayout();
        $this->getResponse()->setBody(
               $this->getLayout()->createBlock('smsnotification/adminhtml_smsnotification_grid')->toHtml()
        );
    }
}

Here is also my config.xml file:

<?xml version="1.0"?>
<config>
  <modules>
    <VivasIndustries_SmsNotification>
      <version>0.1.0</version>
    </VivasIndustries_SmsNotification>
  </modules>
  <global>
    <models>
        <smsnotification>
            <class>VivasIndustries_SmsNotification_Model</class>
            <resourceModel>vivasindustries_smsnotification_resource</resourceModel>
        </smsnotification>
        <vivasindustries_smsnotification_resource>
        <class>VivasIndustries_SmsNotification_Model_Resource</class>
        <entities>
            <smsnotification>
            <table>VivasIndustries_SmsNotification</table>
            </smsnotification>
        </entities>
        </vivasindustries_smsnotification_resource>
    </models>
    <resources>
        <smsnotification_setup>
            <setup>
                <module>VivasIndustries_SmsNotification</module>
            </setup>
            <connection>
                 <use>core_setup</use>
             </connection>
        </smsnotification_setup>
        <smsnotification_read>
            <connection>
                <use>core_read</use>
            </connection>
        </smsnotification_read>
        <smsnotification_write>
            <connection>
                <use>core_write</use>
            </connection>
        </smsnotification_write>
    </resources>    
    <events>
        <sales_order_save_after>
            <observers>
                <vivasindustries_smsnotification>
                    <class>smsnotification/observer</class>
                    <method>orderSaved</method>
                </vivasindustries_smsnotification>
            </observers>
        </sales_order_save_after>
    </events>
    <helpers>
        <smsnotification>
            <class>VivasIndustries_SmsNotification_Helper</class>
        </smsnotification>
    </helpers>
    <blocks>
        <smsnotification>
             <class>VivasIndustries_SmsNotification_Block</class>
        </smsnotification>
    </blocks>
  </global>
  <adminhtml>
    <acl>
        <resources>
            <all>
                <title>Allow Everything</title>
            </all>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <vivas>
                                        <title>Vivas - All</title>
                                    </vivas>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>
    <layout>
        <updates>
            <smsnotification>
                <file>smsnotification.xml</file>
            </smsnotification>
        </updates>
    </layout>   
    </adminhtml>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <VivasIndustries_SmsNotification before="Mage_Adminhtml">VivasIndustries_SmsNotification_Adminhtml</VivasIndustries_SmsNotification>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>  

Database structure:

enter image description here

I've done much of all i've done by this guide: http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/custom_module_with_custom_database_table

Can you please tell me why my Save Item button is not working, where is the problem and how i can fix it ?

Thanks in advance!

NOTE2: Picture of grid table after saving new item.

enter image description here

1
  • I have issues with your use of double underscore constructor in your classes. I believe this would prevent the magento constructor of the parent classed from firing.
    – fexio
    Commented Nov 12, 2015 at 10:20

2 Answers 2

4

Try moving your setForm to the bottom above the call to parent. I added setUseContainer as well. See if this works for you.

The setUseContainer outputs the <form> tags so if your button isn't working then this may be the cause.

<?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', array('id' => $this->getRequest()->getParam('id'))),
                                    'method' => 'post',
                                 ));


          // This moved to the bottom
          //$this->setForm($form);
          ////

$fieldset = $form->addFieldset('smsnotification_form', array('legend'=>Mage::helper('smsnotification')->__('Item information')));

            $fieldset->addField('Receiver', 'text', array(
                'label'     => Mage::helper('smsnotification')->__('Receiver'),
                'class'     => 'required-entry',
                'required'  => true,
                'name'      => 'Receiver',
                 ));

            $fieldset->addField('Phone', 'text', array(
                'label'     => Mage::helper('smsnotification')->__('Phone'),
                'class'     => 'required-entry',
                'required'  => true,
                'name'      => 'Phone',
                ));

            $fieldset->addField('Date', 'text', array(
                'label'     => Mage::helper('smsnotification')->__('Date'),
                'class'     => 'required-entry',
                'required'  => true,
                'name'      => 'Date',
                ));

            if ( Mage::getSingleton('adminhtml/session')->getsmsnotificationData() )
                {
                    $form->setValues(Mage::getSingleton('adminhtml/session')->getsmsnotificationData());
                    Mage::getSingleton('adminhtml/session')->setsmsnotificationData(null);
                } elseif ( Mage::registry('smsnotification_data') ) {
                    $form->setValues(Mage::registry('smsnotification_data')->getData());
                }


                // Add these two lines

                $form->setUseContainer(true);
                $this->setForm($form);

                ////

                return parent::_prepareForm();
        }
}

Try replacing this in your controller:

 $smsnotificationModel = Mage::getModel('smsnotification/smsnotification')->load($this->getRequest()->getParam('id')));

 $smsnotificationModel->setReceiver($postData['Receiver'])
                ->setPhone($postData['Phone'])
                ->setDate($postData['Date'])
                ->save();
8
  • As always you are the Magician! That seems to be working but now i get error like SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE (id='10')' at line 1 Any suggestion or more information needed? Commented Feb 16, 2015 at 18:46
  • I may need more info, but in your saveAction, try changing $smsnotificationModel->setId($this->getRequest()->getParam('id')) to $smsnotificationModel->setData('ID',$this->getRequest()->getParam('id'))). I seem to remember that your field was ID and not id in your database structure but I could be mistaken. Commented Feb 16, 2015 at 18:54
  • I've added an image of the table structure and the ID is not ÌDbut it is id`. Tell me which files i must show you? Thanks in advance! Commented Feb 16, 2015 at 19:41
  • See my edit at the bottom of the answer Commented Feb 16, 2015 at 19:49
  • Thanks once again! Now it says Item was successfully saved but it actually is not making the change to the result. It is not making the changes when it says it does! Commented Feb 16, 2015 at 19:54
1

The issue seem to be with your html form tags don't exist or action is blank... View your browser source and look for a form with id='edit_form`

VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Edit_Form should be more like

class <Namespace>_<Module>_Block_Adminhtml_<Module>_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form(array(
                                        'id' => 'edit_form',
                                        'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
                                        'method' => 'post',
                                     )
        );

        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

<Namespace>_<Module>_Block_Adminhtml_<Module>_Edit_Tab_Form with your form field

Also change this

$this->_objectId = 'id';
$this->_blockGroup = 'smsnotification';
$this->_controller = 'adminhtml_sms_status';

to

$this->_objectId = 'id';
$this->_blockGroup = 'smsnotification';
$this->_controller = 'adminhtml_smsnotification';
9
  • When i change it i get this error: Fatal error: Call to a member function setData() on a non-object in public_html/store/app/code/core/Mage/Adminhtml/Block/Widget/Form/Container.php on line 129 Commented Feb 16, 2015 at 1:52
  • Can you please help me out resolve the problem ? Thanks! Commented Feb 16, 2015 at 12:57
  • Did you check you js error console? Could you inspect element what is the href for the save button? Commented Feb 16, 2015 at 14:25
  • Here is the source code of the button after inspect element: pastebin.com/gVewzdqt Also when i open the console and i click on the button nothing happens. No errors are given to the console. Commented Feb 16, 2015 at 15:41
  • Do you think i have the answer here will resolve my problem: magento.stackexchange.com/questions/15141/… ? Commented Feb 16, 2015 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.