0

我的 action.class.php

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

我的库/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);
}

}

?>

当我提交表单时,出现错误:

致命错误:在...中的非对象上调用成员函数 bind()

坦帕数据:shoppinglist%5Bshoppinglist%5D=liste+15%0D%0A shoppinglist%5Baction_id%5D=1 shoppinglist%5Blist_id%5D=15

为什么?解决方案?

4

2 回答 2

3

仅当请求为 GET 时才创建表单,仅当请求为 POST 时才绑定它。有你的问题:如果它是一个帖子,表单还没有被实例化,因此 $this->formShoppingList 是空的。

于 2012-06-10T15:50:24.947 回答
2

您需要在它ShoppingListForm之前创建一个实例bind

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
    }
}
于 2012-06-10T15:51:36.787 回答