Magento Admin Form – Post Data Missing Issue

adminformpost-data

I am developing a custom magento (1.9) module and have created a form in the admin to add / edit an entity called "subscription". This will either create a new subscription object or edit an existing subscription object.

Following is my SubscriptionController.php

<?php

class Beyondroid_Subscribepro_Adminhtml_SubscriptionsController extends Mage_Adminhtml_Controller_Action {

    public function indexAction() {
        $subscriptionsBlock = $this->getLayout()
            ->createBlock('beyondroid_subscribepro_adminhtml/subscriptions');

        $this->loadLayout();
        $this->_setActiveMenu('beyondroid_subscribepro');
        $this->_addContent($subscriptionsBlock);
        $this->getLayout()->getBlock('head')->setTitle($this->__('Beyondroid SubscribePro - Subscriptions'));
        $this->renderLayout();
    }

    public function editAction() {
        $subscription = Mage::getModel('subscribepro/subscriptions');
        if ($subscriptionId = $this->getRequest()->getParam('id', false)) {
            $subscription->load($subscriptionId);
            if (!($subscription->getId())) {
                $this->_getSession()->addError(
                    $this->__('This subscription no longer exists.')
                );
            }
        }

        //var_dump($this->getRequest()->getPost());
        /**
        if ($postData = $this->getRequest()->getPost('subscriptionsData')) {
            try {
                $subscription->addData($postData);
                $subscription->save();

                $this->_getSession()->addSuccess(
                    $this->__('Subscription data was updated')
                );
                return $this->_redirect(
                    'adminhtml/subscriptions/edit', array('id' => $subscription->getId())
                );
            } catch (Exception $e) {
                Mage::logException($e);
                $this->_getSession->addError($e->getMessage());
            }
        }
        **/
        Mage::register('current_subscription', $subscription);
        $subscriptionEditBlock = $this->getLayout()->createBlock(
            'beyondroid_subscribepro_adminhtml/subscriptions_edit'
        );

        $this->loadLayout()
            ->_addContent($subscriptionEditBlock)
            ->renderLayout();
    }

    public function saveAction() {
        var_dump($this->getRequest()->getPost());
        die();
    }

    protected function _isAllowed() {
        $actionName = $this->getRequest()->getActionName();
        $adminSession = Mage::getSingleton('admin/session');
        $isAllowed = $adminSession->isAllowed('beyondroid_subscribepro/subscriptions');
        return $isAllowed;
    }
}

and here is the Subscriptions/Edit/Form.php

class Beyondroid_Subscribepro_Block_Adminhtml_Subscriptions_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',
            'enctype' => 'multipart/form-data',
        ));
        $form->setUseContainer(true);

        $fieldset = $form->addFieldSet(
            'general',
            array('legend' => 'Subscription Details')
        );

        $currentSubscription = $this->_getSubscription();

        $fieldset->addField('product_id', 'text', array(
            'label' => 'Product ID',
            'required' => true,
            'note' => 'This resembles the product ID for this subscription',
        ));
        $fieldset->addField('deliveries', 'text', array(
            'label' => 'No. of deliveries (in a month)',
            'required' => true,
            'note' => 'This resembles the number of deliveries to be made for this kind of a subscription in one month',
        ));
        $fieldset->addField('unit_price', 'text', array(
            'label' => 'Unit Price',
            'required' => true,
            'note' => 'This defines how much each delivery costs',
        ));

        $form->setValues($currentSubscription->getData());
        $this->setForm($form);
        return parent::_prepareForm();
    }

    protected function _getSubscription() {
        if (!$this->hasData('subscriptions')) {
            $subscription = Mage::registry('current_subscription');
            if (!$subscription instanceof
                    Beyondroid_Subscribepro_Model_Subscriptions) {
                $subscription = Mage::getModel(
                    'beyondroid_subscribepro/subscriptions'
                );
            }
            $this->setData('subscription', $subscription);
        }
        return $this->getData('subscription');
    }
}

Form Loaded

The form gets loaded properly in both new/edit cases (as can be seen in the image above). The problem is when I try to save the form, I do not get any post data. Clicking the save button does route to the saveAction method in my controller but the post data is an array with only 1 key value pair i.e. form_key => 'some random string'.

Following is what var_dump statement in saveAction method returns:

No Post Data

Any Ideas what am I missing here ?

Update

Following is what I see in the request payload i.e. missing post data :O

enter image description here

Update 2

The html structure of the form looks fine. The fields are inside the <form></form> tags and only 1 form on the page.

enter image description here

Best Answer

On your latest screenshot I see that the fields you are submitting don't have a name.

Look at the input with the id product_id for example. the name attribute is empty.

I think the problem comes from the way you defined your fields.

Let's take this for example:

   $fieldset->addField('product_id', 'text', array(
        'label' => 'Product ID',
        'required' => true,
        'note' => 'This resembles the product ID for this subscription',
    ));

There is no name attribute in the configuration array.
it should be something like this:

   $fieldset->addField('product_id', 'text', array(
        'name' => 'product_id', //you are missing this in your code
        'label' => 'Product ID',
        'required' => true,
        'note' => 'This resembles the product ID for this subscription',
    ));

do the same for the other fields and try again.

Related Topic