我正在使用Symfony2。 我有一個EMPLOYEE實體(其中有一個字符串字段'category')和一個CONTRACT實體。如何通過變量在表單上添加if條件
所以,這裏是我的問題:
編輯僱員後,我可以編輯他的合同。
如果員工屬於=='worker'類別,並且如果category ='CEO',我想在合同表單中添加字段「salary」,但我不想顯示該字段。
這裏是我的合約類型:
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"
我堅持這個錯誤,我不明白什麼是錯的在我的代碼
非常感謝您的幫助!