lop*_*ded 20 forms option symfony
我的问题基本上是,是否可以从父表单更改嵌入式字段的选项?
为了说明问题,考虑一下; 我有一个父表单类型类,如下所示:
class FruitFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('apple', new AppleFormType())
;
}
Run Code Online (Sandbox Code Playgroud)
和一个子表单类型,在一个单独的包中,我宁愿不编辑,像这样:
class AppleFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('qty', 'integer', array('label' => 'rubbish label')
;
}
Run Code Online (Sandbox Code Playgroud)
我想把标签更改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')))
;
}
Run Code Online (Sandbox Code Playgroud)
要么:
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');
}
Run Code Online (Sandbox Code Playgroud)
但这些(以及其他一些尝试)都没有让我失望.setOption我看不到一种方法.
有谁知道这样做的方法?
谢谢
Pet*_*ter 40
我还想更改选项,即FOSUserBundle中现有字段的明显"更改标签"案例.我知道我可以在Twig或翻译中做到这一点.
@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
Run Code Online (Sandbox Code Playgroud)
pea*_*mak 10
非常感谢@Peter Wooster.在Symfony 3.0中,我不得不使用一些不同的东西:
我有一个自定义表单类型添加字段,如下所示:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('my_field', ChoiceType::class, [
// Some options here...
'multiple' => false,
]);
}
Run Code Online (Sandbox Code Playgroud)
在另一个自定义表单类型中,我需要扩展上面的类型并更改一些选项:
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);
}
Run Code Online (Sandbox Code Playgroud)
感谢上面的答案,我希望我的帮助!
尝试这样的事情:
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';
);
}
}
Run Code Online (Sandbox Code Playgroud)
和:
class FruitFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('apple', new AppleFormType(), array('qtyLabel' => 'better label'))
;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22694 次 |
| 最近记录: |