自定义符号小部件出错


Error on custom symfony widget

我想编写一个从sfFormWidgetChoice扩展而来的自定义小部件。然而,我发现symfony文档上的指南有点令人困惑。我觉得他们只是把完成的代码扔给我们。http://www.symfony-project.org/more-wit.验证器

我得到这个错误特别:未定义的索引:Renderer_options

我知道我做错了什么。我只是不知道我需要在sfFormWidgetChoice上覆盖什么。

有可能创建一个不需要任何参数的小部件吗?您能提供如何创建自定义小部件的步骤吗?

创建自定义小部件时,通常会覆盖configure(添加自定义选项)和render(修改小部件的呈现)方法。

我认为您得到这个错误是因为您没有调用父类的configure方法:

// in YourCustomWidget class 
protected function configure($options = array(), $attributes = array())
{
    parent::configure($options, $attributes);
    // add your options here
    $this->addRequiredOption('your_mandatory_option');
    $this->addOption('your_optional_option', 'default_value');
}

sfWidgetFormChoice只有一个必需的选项(您称之为"参数")choices的数组,因此您应该在创建小部件时传递它:

// in your form class
$this->setWidget('your_field', new YourCustomWidget(array(
    `choices` => array('a' => 'a', 'b' => 'b', 'c' => 'c')
)));

要自定义小部件的呈现,请覆盖呈现方法(如文档中所示),实现一个自定义逻辑。