我正在使用 EasyAdminBundle 和 Symfony 3.0 并且有一个字段“状态”,默认情况下是文本字段,我希望它是选择类型,但能够使用 javascript 在更改事件上显示或隐藏其他字段。问题是该字段已呈现,但不知何故它不会在更改选项时隐藏字段。另外,我不确定我的方法是否好,因为 symfony 文档不够清晰,而且对我来说,他们的方法不起作用。
所以,代码看起来像这样
MyBundle/控制器/AdminController
public function createEditForm($entity, array $entityProperties)
{
$editForm = parent::createEditForm($entity, $entityProperties);
if($entity instanceof Employee){
$editForm->remove('status');
$editForm->add('status', ChoiceType::class, array(
'choices' => array(
'Working' => "active",
'Not working' => 'inactive'
)
));
}
return $editForm;
}
public function createNewForm($entity, array $entityProperties)
{
$newForm = parent::createNewForm($entity, $entityProperties);
if($entity instanceof Employee){
$newForm->remove('status');
$newForm->add('status', ChoiceType::class, array(
'choices' => array(
'Choose an option' => null,
'Working' => true,
'Not working' => false
)
));
}
return $newForm;
}
MyBundle/Resources/views/Form/employeestatusfield.html.twig
{% extends 'EasyAdminBundle:default:new.html.twig' %}
{% form_theme form 'EmployeeBundle:Form:employeestatusfield.html.twig' %}
{% block _employee_status_widget %}
<script type="text/javascript">
alert('tiiw');
$(document).ready(function () {
toggleFields();
$("#employee_status").change(function () {
toggleFields();
});
});
function toggleFields() {
if ($("#employee_status").valueOf() == 1) {
$("#employee_quitAt").hide();
}
else
$("#employee_quitAt").show();
}
</script>
{% endblock %}
谢谢您的帮助。