2012-06-10 114 views
0

我action.class.php調用一個成員函數bind()的一個非對象在

if ($this->getRequest()->getMethod() == sfRequest::GET) { 
       $this->formShoppingList = new ShoppingListForm(array(
         'shoppinglist' => $items, 
          ), array('shoppinglist_id' => $list_id)); 
} 

    if ($request->isMethod('post')) { 
     $this->formShoppingList->bind($request->getParameter('shoppinglist')); 
    print_r($this->fromShoppingList; 
    } 

我的lib/ShoppingListForm.php:

<?php 

class ShoppingListForm extends BaseForm { 

public function configure() { 
    $shoppinglist_id = $this->getOption('shoppinglist_id'); 

    $this->setWidgets(array(
     'shoppinglist' => new sfWidgetFormTextarea(array(), array('rows' => '10', 'cols' => '35')), 
     'action_id' => new sfWidgetFormInputHidden(array()), 
     'list_id' => new sfWidgetFormInputHidden(array(),array('value' => $shoppinglist_id)), 
    )); 

    $this->widgetSchema->setLabels(array(
     'shoppinglist' => '', 
    )); 


    $this->setValidators(array(
     'shoppinglist' => new sfValidatorString(array('max_length' => 5000), array(
      'required' => 'ShoppingList is empty.' 

     )), 

     'action_id' => new sfValidatorString(array('required' => false)), 
     'list_id' => new sfValidatorString(array('required' => false)) 

    )); 

    $this->widgetSchema->setNameFormat('shoppinglist[%s]'); 

    gfFormHelper::addRequiredToLabel($this); 
} 

} 

?> 

當我提交表單,錯誤即將到來:

Fatal error: Call to a member function bind() on a non-object in....

Tampa-Data: shoppinglist%5Bshoppinglist%5D=liste+15%0D%0A shoppinglist%5Baction_id%5D=1 shoppinglist%5Blist_id%5D=15

爲什麼?解?

回答

2

您需要您bind它來創建ShoppingListForm的實例:

if ($request->isMethod('post')) 
{ 
    // Create instance of ShoppingListForm here 
    $this->formShoppingList = new ShoppingListForm(); 
    $this->formShoppingList->bind($request->getParameter('shoppinglist')); 
    if ($this->formShoppingList->isValid()) 
    { 
    // do something with the submitted data 
    } 
} 
3

如果請求是GET,那麼只能創建表單,並且只有在請求是POST時才綁定它。有你的問題:如果它是一個職位,表單尚未實例化,因此$ this-> formShoppingList爲空。

相關問題