Magento - 尝试创建编辑管理表单,但它在表单中提供空白输入字段

时间:2015-02-14 20:52:08

标签: php magento

我正在使用Magento 1.9.0.1。

我正在开发自定义Magento扩展程序,到目前为止,我已在管理面板中创建了一些自定义页面。在其中一个中我添加了一个网格表,它从自定义MySQL表中获取结果。

让我向您展示此页面的屏幕截图:

enter image description here

问题出现在哪里,当打开编辑表单时,其中的输入字段为空。它们一定不能为空......因为你知道这是编辑表格,必须要编辑一个数据!

以下是截图:

enter image description here

正如你所看到的那些字段是空的......这就是问题......

让我向您展示我认为对解决问题很重要的所有代码。

以下是我的内容:/app/code/community/VivasIndustries/SmsNotification/etc/config.xml:

<?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>  

以下是我所拥有的:/app/code/community/VivasIndustries/SmsNotification/controllers/Adminhtml/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 gridAction()
    {
        $this->loadLayout();
        $this->getResponse()->setBody(
            $this->getLayout()->createBlock('smsnotification/adminhtml_sms_status_grid')->toHtml()
        );
    }

   public function newAction()
    {  
        $this->loadLayout();
        $this->_setActiveMenu('vivassms');
        $this->renderLayout();
    }  



    public function editAction()
    {
        /**
         * Retrieve existing brand data if an ID was specified.
         * If not, we will have an empty brand entity ready to be populated.
         */
        $brand = Mage::getModel('smsnotification/smsnotification');



        // process $_POST data if the form was submitted
        if ($postData = $this->getRequest()->getPost('smsData')) {
            try {
                $brand->addData($postData);
                $brand->save();

                $this->_getSession()->addSuccess(
                    $this->__('The brand has been saved.')
                );

                // redirect to remove $_POST data from the request
                return $this->_redirect(
                    'smsorderstatuses/edit',
                    array('id' => $brand->getId())
                );
            } catch (Exception $e) {
                Mage::logException($e);
                $this->_getSession()->addError($e->getMessage());
            }

            /**
             * If we get to here, then something went wrong. Continue to
             * render the page as before, the difference this time being
             * that the submitted $_POST data is available.
             */
        }

        // Make the current brand object available to blocks.
        Mage::register('sms_brand', $brand);

        // Instantiate the form container.
        $brandEditBlock = $this->getLayout()->createBlock(
            'smsnotification/adminhtml_sms_status_edit'
        );

        // Add the form container as the only item on this page.
        $this->loadLayout()
            ->_addContent($brandEditBlock)
            ->renderLayout();
    }

    public function deleteAction()
    {
        $smsnotification = Mage::getModel('vivasindustries_smsnotification/smsnotification');

        if ($smsnotificationId = $this->getRequest()->getParam('id', false)) {
            $smsnotification->load($smsnotificationId);
        }

        if (!$smsnotification->getId()) {
            $this->_getSession()->addError(
                $this->__('This smsnotification no longer exists.')
            );
            return $this->_redirect(
                '*/*/index'
            );
        }

        try {
            $smsnotification->delete();

            $this->_getSession()->addSuccess(
                $this->__('The smsnotification has been deleted.')
            );
        } catch (Exception $e) {
            Mage::logException($e);
            $this->_getSession()->addError($e->getMessage());
        }

        return $this->_redirect(
            '*/*/index'
        );
    }   

    protected function _initAction()
    {
        $this->loadLayout()
            // Make the active menu match the menu config nodes (without 'children' inbetween)
            ->_setActiveMenu('vivassms')
            ->_title($this->__('SMS Rule'))->_title($this->__('SMS Center'))
            ->_addBreadcrumb($this->__('SMS Rule'), $this->__('SMS Rule'));

        return $this;
    }


    protected function _isAllowed()
    {
        return Mage::getSingleton('admin/session')->isAllowed('sales/foo_bar_baz');
    }
}

以下是我所拥有的:/app/code/community/VivasIndustries/SmsNotification/Model/Smsnotification.php:

<?php
class VivasIndustries_SmsNotification_Model_Smsnotification extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('smsnotification/smsnotification');
    }

}

以下是我所拥有的:/app/code/community/VivasIndustries/SmsNotification/Model/Resource/Smsnotification.php:

<?php
class VivasIndustries_SmsNotification_Model_Resource_Smsnotification extends Mage_Core_Model_Resource_Db_Abstract
{
    /**
     * Initialize resource model
     *
     * @return void
     */
    public function _construct()
    {
        $this->_init('smsnotification/smsnotification','id');
    }
}

以下是我的内容:/app/code/community/VivasIndustries/SmsNotification/Model/Resource/Smsnotification/Collection.php:

<?php 
class VivasIndustries_SmsNotification_Model_Resource_Smsnotification_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract{
    protected function _construct(){
        $this->_init('smsnotification/smsnotification');    
    }
}

以下是我所拥有的:/app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status.php:

<?php

class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        $this->_blockGroup = 'smsnotification';
        $this->_controller = 'adminhtml_sms_status';
        $this->_headerText = Mage::helper('smsnotification')->__('Send SMS on Order Status Changes');
        $this->_addButtonLabel = Mage::helper('smsnotification')->__('Create new SMS Rule');
        parent::__construct();
    }

    protected function _prepareLayout()
    {
        $this->setChild( 'grid',
            $this->getLayout()->createBlock( $this->_blockGroup.'/' . $this->_controller . '_grid',
                $this->_controller . '.grid')->setSaveParametersInSession(true) );
        return parent::_prepareLayout();
    }



}

以下是我所拥有的:/app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status/Grid.php:

<?php

class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('smsnotification_grid');
        $this->setDefaultSort('id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }


    protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('smsnotification/smsnotification_collection');
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }


    protected function _prepareColumns()
    {
          $this->addColumn('id', array(
              'header'    => Mage::helper('smsnotification')->__('ID'),
              'align'     =>'right',
              'width'     => '50px',
              'index'     => 'id',
          ));

          $this->addColumn('Receiver', array(
              'header'    => Mage::helper('smsnotification')->__('Receiver'),
              'align'     =>'left',
              'index'     => 'Receiver',
          ));

        $this->addColumn('Phone', array(
            'header'    => Mage::helper('smsnotification')->__('Phone'),
            'align'     =>'left',
            'index'     => 'Phone',
        ));

        $this->addColumn('Date', array(
            'header'    => Mage::helper('smsnotification')->__('Date'),
            'align'     =>'left',
            'index'     => 'Date',

        ));


        return parent::_prepareColumns();
    }

    public function getRowUrl($row)
    {
        return $this->getUrl('*/*/edit', array('id'=>$row->getId()));
    }
}

以下是我所拥有的:/app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status/Edit.php:

<?php
class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    protected function _construct()
    {
        $this->_blockGroup = 'smsnotification';
        $this->_controller = 'adminhtml_sms_status';


        $this->_mode = 'edit';

        $newOrEdit = $this->getRequest()->getParam('id')
            ? $this->__('Edit')
            : $this->__('New');
        $this->_headerText =  $newOrEdit . ' ' . $this->__('SMS Rule');
    }
}

以下是我所拥有的:/app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status/Edit/Form.php:

<?php
class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        // Instantiate a new form to display our brand for editing.
        $form = new Varien_Data_Form(array(
            'id' => 'edit_form',
            'action' => $this->getUrl(
                'smsorderstatuses/edit',
                array(
                    '_current' => true,
                    'continue' => 0,
                )
            ),
            'method' => 'post',
        ));
        $form->setUseContainer(true);
        $this->setForm($form);

        // Define a new fieldset. We need only one for our simple entity.
        $fieldset = $form->addFieldset(
            'general',
            array(
                'legend' => $this->__('Brand Details')
            )
        );

        $brandSingleton = Mage::getSingleton(
            'smsnotification/smsnotification'
        );

        // Add the fields that we want to be editable.
        $this->_addFieldsToFieldset($fieldset, array(
            'Receiver' => array(
                'label' => $this->__('Receiver'),
                'input' => 'text',
                'required' => true,
            ),
            'Phone' => array(
                'label' => $this->__('Phone'),
                'input' => 'text',
                'required' => true,
            ),
            'Date' => array(
                'label' => $this->__('Date'),
                'input' => 'text',
                'required' => true,
            ),
        ));

        return $this;
    }

    protected function _addFieldsToFieldset(
        Varien_Data_Form_Element_Fieldset $fieldset, $fields)
    {
        $requestData = new Varien_Object($this->getRequest()
            ->getPost('smsnotificationData'));

        foreach ($fields as $name => $_data) {
            if ($requestValue = $requestData->getData($name)) {
                $_data['value'] = $requestValue;
            }

            // Wrap all fields with brandData group.
            $_data['name'] = "smsnotificationData[$name]";

            // Generally, label and title are always the same.
            $_data['title'] = $_data['label'];

            // If no new value exists, use the existing brand data.
            if (!array_key_exists('value', $_data)) {
                $_data['value'] = $this->_getSms()->getData($name);
            }

            // Finally, call vanilla functionality to add field.
            $fieldset->addField($name, $_data['input'], $_data);
        }

        return $this;
    }

    protected function _getSms()
    {
        if (!$this->hasData('smsnotification')) {
            // This will have been set in the controller.
            $smsnotification = Mage::registry('sms_brand');

            // Just in case the controller does not register the brand.
            if (!$smsnotification instanceof
                    VivasIndustries_SmsNotification_Model_Smsnotification) {
                $smsnotification = Mage::getModel(
                    'smsnotification/smsnotification'
                );
            }

            $this->setData('smsnotification', $smsnotification);
        }

        return $this->getData('smsnotification');
    }
}

所以请告诉我我在哪个文件中有错误,因此它显示空字段。

我该如何解决这个问题?

提前致谢!

2 个答案:

答案 0 :(得分:1)

我认为你在控制器中的editAction()中做得更多。你可以在grid.php文件中看到下面的代码

 public function getRowUrl($row)
{
    return $this->getUrl('*/*/edit', array('id'=>$row->getId()));
}

这里返回行的id。您需要使用此ID&#39;。

$id = $this->getRequest()->getParam('id');

从模型中获取数据。

您需要做的实际事情是在您的

  

/app/code/community/VivasIndustries/SmsNotification/Block/Adminhtml/Sms/Status/Edit/Form.php

在文件末尾放置此代码

$id = Mage::app()->getRequest()->getParam('id');
$model= Mage::getModel('smsnotification/smsnotification')->load($id);
  $form->setValues($model->getData());

试试这个。希望这会有所帮助。

答案 1 :(得分:0)

您应该为Grid.php上的每个字段添加一个属性'index'

并在form.php上创建一个包含此索引的字段: 为例:

Grid.php:

$this->addColumn('id_provider', array(
        'header' => 'Provider',
        'align' => 'left',
        **'index' => 'id_provider',**


    ));

在form.php上:

$fieldset->addField(**'id_provider'**, 'text', array(
        'label' => 'Provider',
        'name' => 'provider',
        'required' => true,

    ));