Magento表单块未呈现

时间:2015-11-04 15:31:35

标签: php magento

我正在尝试基于magento模型制作一个真正的网格。 一切都在阅读部分运行良好,但编辑的表单没有呈现,我在日志中没有错误。 我注意到我的_prepareForm函数从未调用过,但我不知道为什么。

我在控制器中的表单调用:

public function editAction()
{
    $this->_initAction();

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

    if ($id) {
        // Load record
        $model->load($id);

        // Check if record is loaded
        if (!$model->getId()) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('This Contact Request no longer exists.'));
            $this->_redirect('*/*/');

            return;
        }
    }

    $this->_title($model->getId() ? $model->getName() : $this->__('New Contact Request'));

    $data = Mage::getSingleton('adminhtml/session')->getContactRequestData(true);
    if (!empty($data)) {
        $model->setData($data);
    }

    Mage::register('whatever_booking', $model);

    $this->_addBreadcrumb($id ? $this->__('Edit Contact Request') : $this->__('New Contact Request'), $id ? $this->__('Edit Contact Request') : $this->__('New Contact Request'));
    $block = $this->getLayout()->createBlock('whatever_booking/adminhtml_contactRequest_edit')->setData('action', $this->getUrl('*/*/save'));
    $this->getLayout()->getBlock('content')->append($block);

    $this->renderLayout();
} 

protected function _initAction()
{
    $this->loadLayout()
        ->_setActiveMenu('customer/ContactRequest')
        ->_title($this->__('Whatever Booking'))->_title($this->__('Contact Request'));

    return $this;
}

我的表格:

class Whatever_Booking_Block_Adminhtml_ContactRequest_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    /**
     * Init class
     */
    public function _construct()
    {
        parent::_construct();
        $this->setId('whatever_booking_contactRequest_form');
        $this->setTitle($this->__('Contact Request Information'));
        //when i var dump here i see that my controller called this function
    }

    protected function _prepareForm()
    {
        var_dump('here');
        die; // this var dump is never reached
    }
}

编辑块

class Whatever_Booking_Block_Adminhtml_ContactRequest_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    /**
     * Init class
     */
    public function _construct()
    {
        $this->_blockGroup = 'whatever_booking';
        $this->_controller = 'adminhtml_contactRequest';

        parent::_construct();

        $this->_updateButton('save', 'label', $this->__('Save Request'));
        $this->_updateButton('delete', 'label', $this->__('Delete Request'));
    }

    /**
     * Get Header text
     *
     * @return string
     */
    public function getHeaderText()
    {
        if (Mage::registry('contact_request')->getId()) {
            return $this->__('Edit Request');
        }
        else {
            return $this->__('New Request');
        }
    }

    protected function _prepareLayout()
    {
        // mage log is passing here when i display one
        return parent::_prepareLayout();
    }

2 个答案:

答案 0 :(得分:1)

快速修复: 在您的控制器中直接加载表单,但是之前通过容器可能更好,您仍然可以这样调用它:

  $this->loadLayout(array('default', 'adminhtml_contactRequest_edit_form'))
       ->_setActiveMenu('customer/ContactRequest');

然后在预订xml文件中进行布局:

<layout>
    <adminhtml_contactRequest_edit_form>
        <reference name="content">
            <block type="whatever_booking/adminhtml_contactRequest_edit_form" name="aeschbachbooking.form" />
        </reference>
    </adminhtml_contactRequest_edit_form>
</layout>

答案 1 :(得分:0)

在您的控制器操作中,您要添加一个编辑块,而不是表单块:

<?php
// ...
    $block = $this->getLayout()->createBlock('whatever_booking/adminhtml_contactRequest_edit')->setData('action', $this->getUrl('*/*/save'));
// ...

Whatever_Booking_Block_Adminhtml_ContactRequest_Edit阻止了Mage_Adminhtml_Block_Widget_Form_Container吗?表单容器负责查找要加载的正确表单。它在_prepareLayout函数中执行此操作:

protected function _prepareLayout()
{
    if ($this->_blockGroup && $this->_controller && $this->_mode) {
        $this->setChild('form', $this->getLayout()->createBlock($this->_blockGroup . '/' . $this->_controller . '_' . $this->_mode . '_form'));
    }
    return parent::_prepareLayout();
}

另外,我想知道驼峰式ContactRequest部分是否会抛出Magento循环。尝试制作块Contactrequest的那一部分并查看是否有效(您必须重命名其文件夹等)。