Php Magento-Adminhtml-空白编辑表单,不在编辑输入字段中显示数据

Php Magento-Adminhtml-空白编辑表单,不在编辑输入字段中显示数据,php,magento,adminhtml,Php,Magento,Adminhtml,我正在使用Magento 1.9.0.1,并且正在使用自定义扩展 现在我被困在创建管理表单的部分。让我给你看看 这是一个自定义页面中的网格表,我在从名为VivasIndustries\u SmsNotification的自定义MySQL表获取数据的管理中创建了该表: 当我点击带有结果的行时,我得到的结果如下: 如您所见,输入字段Receiver、Phone和Date为空,但不能为空。这是一个编辑表单,这些字段必须保存打印在表格行中的数据。另外,当我点击save时,它应该保存编辑的数据,但它不

我正在使用Magento 1.9.0.1,并且正在使用自定义扩展

现在我被困在创建管理表单的部分。让我给你看看

这是一个自定义页面中的网格表,我在从名为
VivasIndustries\u SmsNotification的自定义MySQL表获取数据的管理中创建了该表:

当我点击带有结果的行时,我得到的结果如下:

如您所见,输入字段
Receiver、Phone和Date
为空,但不能为空。这是一个编辑表单,这些字段必须保存打印在表格行中的数据。另外,当我点击save时,它应该保存编辑的数据,但它不工作

让我向大家展示我迄今为止所做的工作:

我在:/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>  

0.1.0
VivasiIndustries\u SmsNotification\u模型
活力工业资源
VivasiIndustries\u SmsNotification\u Model\u资源
活力工业
活力工业
核心单元设置
核心读取
核心写入
smsnotification/观察员
订单保存
VivasiIndustries\u SmsNotification\u助手
VivasiIndustries\u SmsNotification\u Block
允许一切
万岁
smsnotification.xml
VivasiIndustries\u SmsNotification\u Adminhtml
以下是我在:/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()
    {  
        $this->_initAction();

        // Get id if available
        $id  = $this->getRequest()->getParam('id');
        $model = Mage::getModel('smsnotification/smsnotification');


        $this->_initAction()
            ->_addBreadcrumb($id ? $this->__('Edit Baz') : $this->__('New Baz'), $id ? $this->__('Edit Baz') : $this->__('New Baz'))
            ->_addContent($this->getLayout()->createBlock('smsnotification/adminhtml_sms_status_edit')->setData('action', $this->getUrl('*/*/save')))
            ->renderLayout();
    }

    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');
    }
}

1]我将向您提供我的代码,其中包含编辑表单中的大部分功能。根据块/模型/辅助对象进行更改。“AI”=名称空间和“Hi=ModuleName”。请在form.php中尝试下面的代码。它将生成包含数据的编辑字段

<?php

class AI_Hi_Block_Adminhtml_Hi_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{

public function __construct()
{
    parent::__construct();
    $this->setId('hi_form');
    $this->setTitle(Mage::helper('hi')->__('User Information'));
}

protected function _prepareForm()
{
    $model = Mage::registry('hi');

    $form = new Varien_Data_Form(
        array('id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post','enctype' => 'multipart/form-data')
    );

    $form->setHtmlIdPrefix('hi');

    $fieldset = $form->addFieldset('base_fieldset', array('legend'=>Mage::helper('hi')->__('User Information'), 'class' => 'fieldset-wide'));

    if ($model->getId()) {
        $fieldset->addField('id', 'hidden', array(
            'name' => 'id',
        ));
    }

    $fieldset->addField('fname', 'text', array(
        'name'      => 'fname',
        'label'     => Mage::helper('hi')->__('First Name'),
        'title'     => Mage::helper('hi')->__('First Name'),
        'required'  => true,
    ));

    $fieldset->addField('lname', 'text', array(
        'name'      => 'lname',
        'label'     => Mage::helper('hi')->__('Last Name'),
        'title'     => Mage::helper('hi')->__('Last Name'),
        'required'  => true,
    ));



    $fieldset->addField('address', 'textarea', array(
        'name'      => 'address',
        'label'     => Mage::helper('hi')->__('Address'),
        'title'     => Mage::helper('hi')->__('Address'),
        'required'  => false
    ));

    $fieldset->addField('country_id', 'select', array(
      'label'     => Mage::helper('hi')->__('Country'),
      'class'     => 'required-entry',
      'required'  => true,
      'name'      => 'country_id',
      'onclick' => "",
      'onchange' => "",
      'value'  => '1',
      'values' => array('India' => 'India','USA' => 'USA', 'UK' => 'UK'),
      'disabled' => false,
      'readonly' => false,
      'tabindex' => 1
    ));

    $fieldset->addField('gender', 'radios', array(
      'label'     => Mage::helper('hi')->__('Gender'),
      'name'      => 'gender',
      'onclick' => "",
      'onchange' => "",
      'value'  => '1',
      'values' => array(
                        array('value'=>'Male','label'=>'Male'),
                        array('value'=>'Female','label'=>'Female'),
                        array('value'=>'Unspecified','label'=>'Unspecified'),
                   ),
      'disabled' => false,
      'readonly' => false,
      'tabindex' => 1
    ));

    $fieldset->addField('hobby', 'checkboxes', array(
      'label'     => Mage::helper('hi')->__('Hobbies'),
      'name'      => 'hobby[]',
      'values' => array(
                        array('value'=>'Music','label'=>'Music'),
                        array('value'=>'Reading','label'=>'Reading'),
                        array('value'=>'Writing','label'=>'Writing'),
                   ),
      'onclick' => "",
      'onchange' => "",
      'value'  => '1',
      'disabled' => false,
      'tabindex' => 1
    ));

    $fieldset->addField('status', 'select', array(
        'label'     => Mage::helper('hi')->__('Status'),
        'title'     => Mage::helper('hi')->__('Status'),
        'name'      => 'status',
        'required'  => true,
        'options'   => array(
            '1' => Mage::helper('hi')->__('Enabled'),
            '0' => Mage::helper('hi')->__('Disabled'),
        ),
    ));

    $fieldset->addField('categories', 'select', array(
        'label'     => Mage::helper('hi')->__('Categories'),
        'title'     => Mage::helper('hi')->__('Categories'),
        'name'      => 'categories',
        'required'  => true,
        'values' => array(
                     array('value'=> Mage::helper('hi')->getAllCategoriesArray(true)),
                         ),
    ));

    if($model->getData('profimage') == "")
    {
        $fieldset->addField('profimage', 'file', array(
            'name'      => 'profimage',
            'label'     => Mage::helper('hi')->__('Image'),
            'title'     => Mage::helper('hi')->__('Image'),
            'required'  => true
        )); 
    }
    else
    {
        $path = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA)."AI_Hi/".$model->getData('profimage');
        $note = 'Browse new image to replace old one.Allowed image type [ "jpg","jpeg","gif","png"]<br/> <a href="'.$path.'" rel="lightbox" onclick="func_loadLightBox(this);return false;" title="'.$model->getTitle().'">
                <img src="'.$path.'" style="width:100px;height:100px;"/></a>';

        $fieldset->addField('profimage', 'file', array(
            'name'      => 'profimage',
            'label'     => Mage::helper('hi')->__('Image'),
            'title'     => Mage::helper('hi')->__('Image'),
            'required'  => false,
            'note'      => $note
        ));

    }


    if (!$model->getId()) 
    {
        $model->setData('status', '1');
    }

    $form->setValues($model->getData());
    $form->setUseContainer(true);
    $this->setForm($form);

    return parent::_prepareForm();
}

}

?>
}

2] 在/Adminhtml/SmsorderstatusesController.php中找不到deleteAction(): 添加下面的代码

public function deleteAction()
{

    try 
    {
        $hiId = $this->getRequest()->getParam('id');
        $hi = Mage::getSingleton('hi/hi')->load($hiId);
        Mage::dispatchEvent('hi_controller_nammer_delete', array('hi' => $hi));
        $hi->delete();
        $this->_getSession()->addSuccess($this->__('User Deleted successfully.'));
    } 
    catch (Exception $e) 
    {
        $this->_getSession()->addError($e->getMessage());
    }

    $this->_redirect('*/*/index');
}
如果您有任何问题,请告诉我。

1)您必须加载模型并将其数据插入表单以显示预填充的值。您需要编辑方法

VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Edit_Form::_prepareForm()
在行之间插入右下方的代码
$form->setUseContainer(true)
$this->setForm($form)

<?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');
    }
}
<?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'] = "brandData[$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('brand')) {
            // This will have been set in the controller.
            $smsnotification = Mage::registry('current_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');
    }
}
<?php

class AI_Hi_Block_Adminhtml_Hi_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{

public function __construct()
{
    parent::__construct();
    $this->setId('hi_form');
    $this->setTitle(Mage::helper('hi')->__('User Information'));
}

protected function _prepareForm()
{
    $model = Mage::registry('hi');

    $form = new Varien_Data_Form(
        array('id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post','enctype' => 'multipart/form-data')
    );

    $form->setHtmlIdPrefix('hi');

    $fieldset = $form->addFieldset('base_fieldset', array('legend'=>Mage::helper('hi')->__('User Information'), 'class' => 'fieldset-wide'));

    if ($model->getId()) {
        $fieldset->addField('id', 'hidden', array(
            'name' => 'id',
        ));
    }

    $fieldset->addField('fname', 'text', array(
        'name'      => 'fname',
        'label'     => Mage::helper('hi')->__('First Name'),
        'title'     => Mage::helper('hi')->__('First Name'),
        'required'  => true,
    ));

    $fieldset->addField('lname', 'text', array(
        'name'      => 'lname',
        'label'     => Mage::helper('hi')->__('Last Name'),
        'title'     => Mage::helper('hi')->__('Last Name'),
        'required'  => true,
    ));



    $fieldset->addField('address', 'textarea', array(
        'name'      => 'address',
        'label'     => Mage::helper('hi')->__('Address'),
        'title'     => Mage::helper('hi')->__('Address'),
        'required'  => false
    ));

    $fieldset->addField('country_id', 'select', array(
      'label'     => Mage::helper('hi')->__('Country'),
      'class'     => 'required-entry',
      'required'  => true,
      'name'      => 'country_id',
      'onclick' => "",
      'onchange' => "",
      'value'  => '1',
      'values' => array('India' => 'India','USA' => 'USA', 'UK' => 'UK'),
      'disabled' => false,
      'readonly' => false,
      'tabindex' => 1
    ));

    $fieldset->addField('gender', 'radios', array(
      'label'     => Mage::helper('hi')->__('Gender'),
      'name'      => 'gender',
      'onclick' => "",
      'onchange' => "",
      'value'  => '1',
      'values' => array(
                        array('value'=>'Male','label'=>'Male'),
                        array('value'=>'Female','label'=>'Female'),
                        array('value'=>'Unspecified','label'=>'Unspecified'),
                   ),
      'disabled' => false,
      'readonly' => false,
      'tabindex' => 1
    ));

    $fieldset->addField('hobby', 'checkboxes', array(
      'label'     => Mage::helper('hi')->__('Hobbies'),
      'name'      => 'hobby[]',
      'values' => array(
                        array('value'=>'Music','label'=>'Music'),
                        array('value'=>'Reading','label'=>'Reading'),
                        array('value'=>'Writing','label'=>'Writing'),
                   ),
      'onclick' => "",
      'onchange' => "",
      'value'  => '1',
      'disabled' => false,
      'tabindex' => 1
    ));

    $fieldset->addField('status', 'select', array(
        'label'     => Mage::helper('hi')->__('Status'),
        'title'     => Mage::helper('hi')->__('Status'),
        'name'      => 'status',
        'required'  => true,
        'options'   => array(
            '1' => Mage::helper('hi')->__('Enabled'),
            '0' => Mage::helper('hi')->__('Disabled'),
        ),
    ));

    $fieldset->addField('categories', 'select', array(
        'label'     => Mage::helper('hi')->__('Categories'),
        'title'     => Mage::helper('hi')->__('Categories'),
        'name'      => 'categories',
        'required'  => true,
        'values' => array(
                     array('value'=> Mage::helper('hi')->getAllCategoriesArray(true)),
                         ),
    ));

    if($model->getData('profimage') == "")
    {
        $fieldset->addField('profimage', 'file', array(
            'name'      => 'profimage',
            'label'     => Mage::helper('hi')->__('Image'),
            'title'     => Mage::helper('hi')->__('Image'),
            'required'  => true
        )); 
    }
    else
    {
        $path = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA)."AI_Hi/".$model->getData('profimage');
        $note = 'Browse new image to replace old one.Allowed image type [ "jpg","jpeg","gif","png"]<br/> <a href="'.$path.'" rel="lightbox" onclick="func_loadLightBox(this);return false;" title="'.$model->getTitle().'">
                <img src="'.$path.'" style="width:100px;height:100px;"/></a>';

        $fieldset->addField('profimage', 'file', array(
            'name'      => 'profimage',
            'label'     => Mage::helper('hi')->__('Image'),
            'title'     => Mage::helper('hi')->__('Image'),
            'required'  => false,
            'note'      => $note
        ));

    }


    if (!$model->getId()) 
    {
        $model->setData('status', '1');
    }

    $form->setValues($model->getData());
    $form->setUseContainer(true);
    $this->setForm($form);

    return parent::_prepareForm();
}

}

?>
public function __construct()
{
    parent::__construct();
    $this->_objectId = 'id';
    $this->_blockGroup = 'hi';
    $this->_controller = 'adminhtml_hi';
    $this->_updateButton('save', 'label', Mage::helper('hi')->__('Save User'));
    $this->_updateButton('delete', 'label', Mage::helper('hi')->__('Delete User'));
    $this->_addButton('saveandcontinue', array(
        'label'     => Mage::helper('adminhtml')->__('Save and Continue Edit'),
        'onclick'   => 'saveAndContinueEdit()',
        'class'     => 'save',
    ), -100);

    $this->_formScripts[] = "
        function saveAndContinueEdit(){
            editForm.submit($('edit_form').action+'back/edit/');
        }
    ";
}

public function getHeaderText()
{
    if (Mage::registry('hi')->getId()) 
    {
        return Mage::helper('hi')->__("Edit User '%s'", $this->htmlEscape(Mage::registry('hi')->getTitle()));
    }
    else 
    {
        return Mage::helper('hi')->__('New User');
    }
}
public function deleteAction()
{

    try 
    {
        $hiId = $this->getRequest()->getParam('id');
        $hi = Mage::getSingleton('hi/hi')->load($hiId);
        Mage::dispatchEvent('hi_controller_nammer_delete', array('hi' => $hi));
        $hi->delete();
        $this->_getSession()->addSuccess($this->__('User Deleted successfully.'));
    } 
    catch (Exception $e) 
    {
        $this->_getSession()->addError($e->getMessage());
    }

    $this->_redirect('*/*/index');
}
VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Status_Edit_Form::_prepareForm()
$id = Mage::app()->getRequest()->getParam('id');
$model = Mage::getModel('smsnotification/smsnotification')->load($id);
$form->setValues($model->getData());