私の質問は基本的には、埋め込みのフィールドのオプションを親フォームから変更することは可能ですか?埋め込みフォームのsymfony2変更フィールドのオプション
問題を説明するには、これを考慮してください。このように、
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' => 'rubbish label')
;
}
と私がしたいですqty
のラベルを別のものに変更するには、AppleForm
が使用されているすべての場所ではなく、FruitForm
でのみこれを実行します。すべての私を失敗している
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');
}
が、これらの(および他の試行回数)のいずれ:
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 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 FruitFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('apple', new AppleFormType(), array('qtyLabel' => 'better label'))
;
}
}
$オプションs = $フィールド - > getOptions();しかし、私にとってはうまくいきませんでしたが、$ field-> getConfig() - > getOptions()はトリックでした!再度、感謝します ! – Steven
私のために働かない –
私のためにうまく働いた。 @Stevenでは、 '$ form-> get(" field ") - > getOptions()'を使う必要があります。そうでない場合、その名前のフィールドはまだ存在しません。 – Adambean