创建表单时出错

时间:2017-03-02 09:12:25

标签: symfony-1.4

我尝试通过引用tutorial

中的指南来创建基本表单

这是我的表格版本

class UsersForm extends BaseUsersForm
{
  public function configure()
  {
      $this->useFields(["name,email"]);

      $this->setWidgetSchema("email") = new sfWidgetFormInputText();
      $this->setWidgetSchema("name") = new sfWidgetFormInputText();

      $this->validatorSchema["email"] = new sfValidatorEmail();
      $this->validatorSchema["name"] =new sfValidatorString(["max_length" => "30"]);

      $this->widgetSchema->setLabels([

          "email" => "Email Address",
          "name" => "User name"

      ]);

  }
}

我收到了这个错误

  

致命错误:在写上下文中不能使用方法返回值

请告诉我,如果我错了代码的任何部分。

1 个答案:

答案 0 :(得分:8)

您的代码中存在多个错误,从以下开始:

$this->useFields(["name,email"]);

你可能想写的地方:

$this->useFields(["name", "email"]);

您获得的致命错误是因为此代码段(您尝试为函数返回值指定值):

$this->setWidgetSchema("email") = new sfWidgetFormInputText();
$this->setWidgetSchema("name") = new sfWidgetFormInputText();

更好的版本是:

$arWidgets = [
   "email" => new sfWidgetFormInputText(),
   "name" => new sfWidgetFormInputText(),
];

$arValidators = [
   "email" => new sfValidatorEmail(),
   "name" => new sfValidatorString(["max_length" => "30"]),
];

$this->setWidgets($arWidgets);
$this->setValidators($arValidators);

应用这些更改后,您的问题应该得到解决。