2016-08-09 5 views
0

私はカスタマイズされたラベルで他のエンティティ属性に基づいて入力をしようとしています。Symfony2で小枝に値を渡す方法

私は$kilos_maxlimの値をクラスのコンストラクタに設定しました。だから、私はそのフィールドを満たしています。私は$kilos_maxlimという値に基づいて、$precioという入力を表示したいと考えています。

私がするClassTypeに、この入力フィールドを持っている:

->add('precio', null, array(
    'attr' => array('autofocus' => true), 
    'label' => 'label.precio', 
)) 

はどのようにして入力されずに値を渡すことができますか?

答えて

2

それは、このような単純な動作するはずです:

public function buildForm(FormBuilderInterface $builder, array $options) { 
    // get the actual entity 
    $entity = $builder->getData(); 

    // set the value as the label 
    $builder->add('precio', null, array(
     'label' => 'label.precio ' . $entity->getKilosMaxlim(), 
    )); 
} 
+0

それは例外:非オブジェクトのメンバ関数getKilosMaxlim()を呼び出します。しかし、私は$エンティティをダンプするときは、正しいクラスのようです。 – jmunozco

+0

私はこれをconfigureOptionsメソッドに持っています:$ resolver-> setDefaults(array( 'data_class' => 'AppBundle \ Entity \ TarifaPeso'、 ));正しいクラスでもある – jmunozco

+0

'$ entity = $ builder-> getData();'の後に 'var_dump($ entity);'を呼び出すと、正しいクラスで正しいクラスが表示されますが、 '$ entity-> getKilosMaxlim () 'が動作しない場合、' $ entity-> getKilosMaxlim() 'にタイプミスがあるようです。 – insertusernamehere

関連する問題