从控制器传递选项到嵌入式 Symfony 表单时选项不存在错误

Option does not exist error when passing through option from controller to embedded Symfony form

当我尝试以我的一种形式将选项传递给我的 buildForm 方法时出现以下错误。

The option "numOfHoles" does not exist. Defined options are: "action", "allow_extra_fields", "attr", "auto_initialize", "block_name", "by_reference", "cascade_validation", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "inherit_data", "intention", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "mapped", "max_length", "method", "pattern", "post_max_size_message", "property_path", "read_only", "required", "translation_domain", "trim", "validation_groups", "virtual".

在我的控制器中:

// hardcoded here for brevity in this example
$form = $this->createForm('crmpicco_course_row', $courseRow, array('numOfHoles' => 18));

crmpicco_course_row表格class中:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'text')
        ->add('course', 'crmpicco_course', array('numOfHoles' => $options['numOfHoles']))
    ;
}

crmpicco_course表格class中:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    for ($i = 0; $i < $options['numOfHoles']; $i++) {
        $builder->add($i, 'text', array('required' => 'false'));
    }
}

/**
 * @return string name
 */
public function getName()
{
    return 'crmpicco_course';
}

谁能看出为什么选项 numOfHoles 没有通过?

尝试这样做:

$numOfHoles = array('numOfHoles' => $options['numOfHoles']));

然后:

$oForm = $this->createForm(new CompanyForm($numOfHoles));

然后你需要在你的buildForm之前调用它:

public function __construct($contacts))
{
$this->vnumOfHoles = $numOfHoles;
}

如您所见,每种表单类型都有一个预定义的选项列表。添加新选项需要稍作调整。实际方法在 Symfony 开发过程中发生了变化,因此您可能会遇到一些旧的折旧解决方案。

此处讨论了最新的解决方案:http://symfony.com/blog/new-in-symfony-2-7-form-and-validator-updates#deprecated-setdefaultoptions-in-favor-of-configureoptions

所以基本上加上

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Whatever',
        'numOfHoles' => 0,
    ));

到您的表单类型,您应该可以开始了。

我在视频中看到您需要为 "invent" 选项创建表单类型扩展:

https://symfonycasts.com/screencast/symfony-forms/form-type-extension#play

“事实证明,您不能只 "invent" 新选项并传递它们:每个字段都有一组具体的有效选项。但是,在 TextareaSizeExtension 中,我们可以发明新选项。”

例如当需要添加行属性时,在扩展中这样做:

   public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $view->vars['attr']['rows'] = $options['rows'];
    }

public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'rows' => 10
        ]);
    }

现在在 buildForm 方法中,当您添加一个字段时,您可以通过这种方式传递选项:

->add('content', null, ['rows' => 15])

这至少应该与视频中谈到的 symfony 4 一起使用,也可能是 3.4。

刚刚观看了另一个视频 https://symfonycasts.com/screencast/symfony-forms/form-options-data,看到了 Cerad 回答的内容。所以现在据我了解,只有当我们想要扩展第三方表单类型时才需要 formExtention。