当选项从控制器传递到嵌入式Symfony形式时,选项不存在错误

问题描述 投票:16回答:3

[尝试在我的一种表单中将选项传递给我的buildForm方法时,出现以下错误。

选项“ numOfHoles”不存在。定义的选项为:“动作”,“ allow_extra_fields”,“ attr”,“ auto_initialize”,“ block_name”,“ by_reference”,“ cascade_validation”,“ compound” ,“约束”,“ csrf_field_name”,“ csrf_message”,“ csrf_protection”,“ csrf_provider”,“ csrf_token_id”,“ csrf_token_manager”,“ data”,“ data_class”,“ disabled”,“ empty_data”,“ error_b” error_mapping”,“ extra_fields_message”,“ inherit_data”,“ intent”,“ 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表单类中:

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

crmpicco_course表单类中:

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没有通过吗?

php forms symfony symfony-forms php-5.6
3个回答
25
投票

您已经发现,每种表单类型都有一个预定义的选项列表。添加新选项需要稍作调整。在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,
    ));

对于您的表单类型,您应该会很好。


0
投票

我在视频中看到,您需要为“发明”选项创建表单类型扩展名:

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

“事实证明,您不能只是”发明“新选项并传递它们:每个字段都有一组具体的有效选项。但是,在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。


-1
投票

尝试这样做:

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

然后:

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

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

public function __construct($contacts))
{
$this->vnumOfHoles = $numOfHoles;
}
© www.soinside.com 2019 - 2024. All rights reserved.