My question basically is, is it possible to change an option of a field of an embedded for from the parent form?
我的问题基本上是,是否可以从父表单中更改嵌入式字段的选项?
To illustrate the problem consider this; I have a parent form type class like this:
为了说明问题,考虑一下;我有一个父表单类型类,如下所示:
class FruitFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('apple', new AppleFormType())
;
}
and a child form type class that is in a separate bundle and I would prefer not to edit, like this:
和一个子表单类型,在一个单独的包中,我宁愿不编辑,如下所示:
class AppleFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('qty', 'integer', array('label' => 'rubbish label')
;
}
and I want to change the label of qty
to something else, but I want to do this only in the FruitForm
, not everywhere where the AppleForm
is used. I had hoped to be able to do something like:
我想将qty的标签更改为其他内容,但我只想在FruitForm中执行此操作,而不是在使用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')))
;
}
or:
要么:
class FruitFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('apple', new AppleFormType())
;
$builder->get('apple')->get('qty')->setOption('label', 'better label');
}
but neither of these (and a number of other attempts) have all failed me. There does not exist a setOption
method that I can see.
但这些(以及其他一些尝试)都没有让我失望。我没有看到的setOption方法。
Does anyone know of a way of doing this?
有谁知道这样做的方法?
Thanks
谢谢
5 个解决方案
#1
39
I also wanted to change options, the obvious "change the label" case for an existing field from the FOSUserBundle. I know I could do this in Twig or with translations.
我还想更改选项,即FOSUserBundle中现有字段的明显“更改标签”案例。我知道我可以在Twig或翻译中做到这一点。
@redbirdo pointed me in the right direction with "it appears that adding a field with the same name will replace it". Here's the solution:
@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
#2
8
Try something like this:
尝试这样的事情:
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';
);
}
}
and:
和:
class FruitFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('apple', new AppleFormType(), array('qtyLabel' => 'better label'))
;
}
}
#3
8
Thanks a lot to @Peter Wooster. In Symfony 3.0, I had to use something a bit different:
非常感谢@Peter Wooster。在Symfony 3.0中,我不得不使用一些不同的东西:
I have a custom Form Type adding fields like so:
我有一个自定义的表单类型添加字段,如下所示:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('my_field', ChoiceType::class, [
// Some options here...
'multiple' => false,
]);
}
And in another custom Form Type I need to extend the Type above and change some options:
在另一个自定义表单类型中,我需要扩展上面的类型并更改一些选项:
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);
}
Thanks to the answers above, I hope mine helps!
感谢上面的答案,我希望我的帮助!
#4
3
I am in a case where I can not access the form builder code but have to override a field options to add a 'required' => true
.
我遇到的情况是我无法访问表单构建器代码但必须覆盖字段选项以添加'required'=> true。
Expanding @peter-wooster and @thedamnedrhino reply to a symfony issue on github (https://github.com/symfony/symfony/issues/11188) , I end up with this piece of code.
扩展@ peter-wooster和@thedamnedrhino回复github上的symfony问题(https://github.com/symfony/symfony/issues/11188),我最终得到了这段代码。
$field = $form->get('combinaisons');
$options = $field->getConfig()->getOptions();
$type = $field->getConfig()->getType()->getName();
$options['required'] = true;
unset($options['em']);
$form->add('combinaisons', $type, $options);
This works fine with symfony/symfony:2.3.21, doctrine/doctrine-bundle:1.2.0 and doctrine/orm:2.3.6
这适用于symfony / symfony:2.3.21,doctrine / doctrine-bundle:1.2.0和doctrine / orm:2.3.6
#5
0
Modifying the view is often a lot easier for this kind of change.
对于这种更改,修改视图通常要容易得多。
$view->vars['label'] = 'New label';
Typically your view will be a parent form, so it might look like this - change from 'Date' > 'Publication date':
通常,您的视图将是父表单,因此它可能如下所示 - 从“日期”>“发布日期”更改:
$view = $form->createView(...);
$view->children['date']->vars['label'] = 'Publication date';
If your form is encapsulated in its own type you can use the finishView function:
如果表单是以自己的类型封装的,则可以使用finishView函数:
public function finishView(FormView $view, FormInterface $form, array $options)
{
$view->children['date']->vars['label'] = 'Publication date';
}
Since most of what is finally passed into the templating engine for rendering is in straight array form, you can mess with quite a lot of stuff at this point.
由于最终传递到模板引擎中的大部分内容都是以直线阵列形式存在的,所以此时你可以搞砸很多东西。
#1
39
I also wanted to change options, the obvious "change the label" case for an existing field from the FOSUserBundle. I know I could do this in Twig or with translations.
我还想更改选项,即FOSUserBundle中现有字段的明显“更改标签”案例。我知道我可以在Twig或翻译中做到这一点。
@redbirdo pointed me in the right direction with "it appears that adding a field with the same name will replace it". Here's the solution:
@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
#2
8
Try something like this:
尝试这样的事情:
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';
);
}
}
and:
和:
class FruitFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('apple', new AppleFormType(), array('qtyLabel' => 'better label'))
;
}
}
#3
8
Thanks a lot to @Peter Wooster. In Symfony 3.0, I had to use something a bit different:
非常感谢@Peter Wooster。在Symfony 3.0中,我不得不使用一些不同的东西:
I have a custom Form Type adding fields like so:
我有一个自定义的表单类型添加字段,如下所示:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('my_field', ChoiceType::class, [
// Some options here...
'multiple' => false,
]);
}
And in another custom Form Type I need to extend the Type above and change some options:
在另一个自定义表单类型中,我需要扩展上面的类型并更改一些选项:
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);
}
Thanks to the answers above, I hope mine helps!
感谢上面的答案,我希望我的帮助!
#4
3
I am in a case where I can not access the form builder code but have to override a field options to add a 'required' => true
.
我遇到的情况是我无法访问表单构建器代码但必须覆盖字段选项以添加'required'=> true。
Expanding @peter-wooster and @thedamnedrhino reply to a symfony issue on github (https://github.com/symfony/symfony/issues/11188) , I end up with this piece of code.
扩展@ peter-wooster和@thedamnedrhino回复github上的symfony问题(https://github.com/symfony/symfony/issues/11188),我最终得到了这段代码。
$field = $form->get('combinaisons');
$options = $field->getConfig()->getOptions();
$type = $field->getConfig()->getType()->getName();
$options['required'] = true;
unset($options['em']);
$form->add('combinaisons', $type, $options);
This works fine with symfony/symfony:2.3.21, doctrine/doctrine-bundle:1.2.0 and doctrine/orm:2.3.6
这适用于symfony / symfony:2.3.21,doctrine / doctrine-bundle:1.2.0和doctrine / orm:2.3.6
#5
0
Modifying the view is often a lot easier for this kind of change.
对于这种更改,修改视图通常要容易得多。
$view->vars['label'] = 'New label';
Typically your view will be a parent form, so it might look like this - change from 'Date' > 'Publication date':
通常,您的视图将是父表单,因此它可能如下所示 - 从“日期”>“发布日期”更改:
$view = $form->createView(...);
$view->children['date']->vars['label'] = 'Publication date';
If your form is encapsulated in its own type you can use the finishView function:
如果表单是以自己的类型封装的,则可以使用finishView函数:
public function finishView(FormView $view, FormInterface $form, array $options)
{
$view->children['date']->vars['label'] = 'Publication date';
}
Since most of what is finally passed into the templating engine for rendering is in straight array form, you can mess with quite a lot of stuff at this point.
由于最终传递到模板引擎中的大部分内容都是以直线阵列形式存在的,所以此时你可以搞砸很多东西。