2

私はSymfony2で作業しています。EMPLOYEE エンティティ (特に文字列フィールド「カテゴリ」を持つ) と CONTRACT エンティティがあります。

だから、ここに私の問題があります:

従業員を編集したら、その従業員の契約を編集できます。

従業員がカテゴリ == 'worker' に属している場合、契約書にフィールド「salary」を追加したいのですが、カテゴリ = 'CEO' の場合、このフィールドを表示したくありません。

ここに私の ContractType があります:

class ContractType extends AbstractType
 {
    protected $employee;

    function __construct(MyBundle\Entity\Employee $employee = null) {
    $this->employee = $employee;
     }


    public function buildForm(FormBuilder $builder, array $options) {
    $builder
        ->add('startDate');

        if ($this->employee !== null && $this->employee->getCategory() == 'worker') 
        {
            $builder
           ->add('salary', 'money', array('currency' => 'USD', 'required' =>false));                            
        }

        elseif ($this->employee !== null && $this->employee->getCategory() == 'CEO') 
        {
            $builder->add('salary', 'hidden', array('required' => false));

        }
    }
}

これが私の contract_form.html.twig です:

 {% if employee.category == 'worker'%}
   <tr>
    <td>{{ form_label(form.salary, "Salary : ") }}</td>
    <td>{{ form_widget(form.salary) }}</td>
   </tr>
 {% endif %}


After editing a employee and setting him category=='worker', when I want to edit him a contract, I have the error :

   Method "salary" for object "Symfony\Component\Form\FormView" does not exist in MyBundle:Contract:contract_form.html.twig"

このエラーで立ち往生しています。コードの何が問題なのかわかりません

助けてくれてどうもありがとう!

4

2 に答える 2

0

buildViewFormView オブジェクトに変数を実装して設定する必要があります。から実行する方法の例を次に示しますCollectionType

于 2012-07-07T12:11:04.293 に答える
0

あなたがしたいことは、従業員エンティティに従って動的に契約書を作成することです。IMO、このユース ケースを解決するフォーム イベントを使用する必要があります。

http://symfony.com/doc/current/cookbook/form/dynamic_form_generation.html

于 2012-07-07T13:33:33.223 に答える