Forms Symfony2更改嵌入表单的字段选项

Forms Symfony2更改嵌入表单的字段选项,forms,symfony,option,Forms,Symfony,Option,我的问题基本上是,是否可以从父窗体更改嵌入式for字段的选项 为了说明问题,考虑一下这个问题;我有这样一个父窗体类型类: class FruitFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name', 'text')

我的问题基本上是,是否可以从父窗体更改嵌入式for字段的选项

为了说明问题,考虑一下这个问题;我有这样一个父窗体类型类:

class FruitFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text')
            ->add('apple', new AppleFormType())
        ;
    }
class AppleFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text')
            ->add('qty', 'integer', array('label' => $options['qtyLabel'])
        ;
    }

    public function getDefaultOptions()
    {
        return array(
            'qtyLabel' = 'rubbish label';
        );
    }
}
还有一个子表单类型类,它位于一个单独的包中,我不希望编辑,如下所示:

class AppleFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text')
            ->add('qty', 'integer', array('label' => 'rubbish label')
        ;
    }
我想把
quantity
的标签改成其他东西,但我只想在
furtiform
中这样做,而不是在使用
AppleForm
的任何地方。我曾希望能够做到以下几点:

class FruitFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text')
            ->add('apple', new AppleFormType(), array('qty' => array('label' => 'better label')))
        ;
    }
或:

但这两次(以及其他多次尝试)都没有让我失望。我看不到
setOption
方法

有人知道这样做的方法吗


谢谢

试试这样的方法:

class FruitFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text')
            ->add('apple', new AppleFormType())
        ;
    }
class AppleFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text')
            ->add('qty', 'integer', array('label' => $options['qtyLabel'])
        ;
    }

    public function getDefaultOptions()
    {
        return array(
            'qtyLabel' = 'rubbish label';
        );
    }
}
以及:


我还想更改选项,即FOSUserBundle中现有字段的明显“更改标签”情况。我知道我可以用小树枝或翻译来做这件事

@redbirdo为我指出了正确的方向,他说“似乎添加一个同名字段将替换它”。以下是解决方案:

$field = $builder->get('username');         // get the field
$options = $field->getOptions();            // get the options
$type = $field->getType()->getName();       // get the name of the type
$options['label'] = "Login Name";           // change the label
$builder->add('username', $type, $options); // replace the field

在这种情况下,我无法访问表单生成器代码,但必须覆盖字段选项以添加
“必需”=>true

扩展@peter wooster和@thedamnedrhino对github()上的一个symfony问题的回复,我最终得到了这段代码

$field = $form->get('combinaisons');
$options = $field->getConfig()->getOptions();
$type = $field->getConfig()->getType()->getName();
$options['required'] = true;
unset($options['em']);
$form->add('combinaisons', $type, $options); 

这适用于symfony/symfony:2.3.21、条令/条令包:1.2.0和条令/orm:2.3.6。对于此类更改,修改视图通常容易得多

$view->vars['label'] = 'New label';
通常,您的视图将是一个父窗体,因此它可能如下所示-从“日期”更改为“发布日期”:

$view = $form->createView(...);
$view->children['date']->vars['label'] = 'Publication date';
如果表单以自己的类型封装,则可以使用finishView函数:

public function finishView(FormView $view, FormInterface $form, array $options)
{
    $view->children['date']->vars['label'] = 'Publication date';
}

由于最终传递到模板引擎进行渲染的大部分内容都是以直接数组的形式进行的,因此在这一点上,您可以处理很多内容。

非常感谢@Peter Wooster。 在Symfony 3.0中,我不得不使用一些不同的东西:

我有一个自定义表单类型,添加如下字段:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('my_field', ChoiceType::class, [
        // Some options here...
        'multiple' => false,
    ]);
}
在另一个自定义表单类型中,我需要扩展上面的类型并更改一些选项:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    parent::buildForm($builder, $options);

    $myField = $builder->get('my_field');
    $fieldOptions = $myField->getOptions();
    // Retrieve the FormType. That is the part that is different.
    $fieldType = get_class($myField->getType()->getInnerType());
    $fieldOptions['multiple'] = true;
    // I can obviously put the name 'my_field' directly here
    $builder->add($myField->getName(), $fieldType, $fieldOptions);
}

多亏了以上的答案,我希望我的答案能有所帮助

感谢您的回答,我知道这是一个选项,但由于
AppleFormType
在供应商包中,我必须覆盖或替换它才能使其正常工作。我希望有一个完全可以在
FruitFormType
类中完成的解决方案。抱歉,我看到您不希望编辑它,但假设您可以覆盖它。是否可以选择$builder->get('apple')->add('qty','integer',array('label'=>'better label')FormBuilder的文档似乎添加一个同名字段将替换它,但显然,然后您必须重新定义所有选项,而不仅仅是标签…编辑-我找到了FormBuilderInterface的API。它有get/setAttribute()。您是否尝试过使用$builder->get('apple')->get('qty')->setAttribute('label'、'better label');?是的,这很理想,但不幸的是'label'是一个选项而不是一个属性。我以前尝试过它,我只是再次测试以确保额外的确定性(仍然没有做任何事情)。我现在开始使用覆盖方法。我喜欢你在上面的评论中建议的添加方法,正如你所说,它有它的缺点,但是是建议的最简单的方法(+1;))更好的方法$options=$field->getOptions();但是,$field->getConfig()->getOptions()对我不起作用这个把戏成功了!再次感谢!对我来说效果很好。@Steven你需要使用
$form->get(“field”)->getOptions()
,否则同名字段还不存在。作为详细说明,我使用的是Symfony 4,$type=$field->getType()->getName();不起作用(getName未定义)。我必须这样做:$type=get\u class($field->->getType()->getInnerType());如果有人有更好的想法,我完全赞成。请注意,此更改仅适用于“当前”视图。这不会更改从中生成视图的形式。但我希望此解决方案将适合许多用例。