2015-11-04 39 views
1

我正在嘗試基於magento模型製作真正的網格。 在閱讀部分,一切都運行良好,但編輯窗體沒有呈現,我在日誌中沒有錯誤。 我注意到我的_prepareForm函數從來沒有被調用,但我不知道爲什麼。Magento Form Block未呈現

我的形式呼叫控制器:

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(); 
    } 
+1

你的控制器,似乎打電話錯過被$ this-> loadlayout(),是在_initaction()? – Mir

+0

我編輯了我的_initAction文章,是的它被稱爲那裏,所以它不是問題:) –

回答

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的一部分,看看是否有效(你將不得不重新命名它的文件夾等)。

+0

嗨,我的Whatever_Booking_Block_Adminhtml_ContactRequest_Edit擴展Mage_Adminhtml_Block_Widget_Form_Container。 我試圖覆蓋_prepareLayout只是爲了看看它是否被調用,是的我的代碼正在通過這個函數(只是調用它的父)在Whatever_Booking_Block_Adminhtml_ContactRequest_Edit –

+0

駱駝案件,我想它傳遞的很好,因爲我來自這種形式從一個網格和它的網格容器具有相同的結構 Whatever_Booking_Block_ContactRequest和Whatever_Booking_Block_Adminhtml_ContactRequest_Grid,所以我認爲這不是問題,要麼Magento沒有帶來這個領域的駱駝案例的問題,它只是不好使用下劃線:) –

+1

你可以包括在你的問題編輯塊? –

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> 
+0

這似乎工作,但正如你所說,它可能會更好地通過容器。我會等待看看是否有人使用容器工作。謝謝 –

+0

另外我沒有容器的按鈕,如果我直接到達窗體 –