Magento Form Block not rendered


Magento Form Block not rendered

我正在尝试基于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();
    }

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

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

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

<?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,看看是否有效(您必须重命名其文件夹等)。