Tôi đã tạo một biểu mẫu với Symfony2 FormBuilder và tôi muốn vô hiệu hóa một trong các trường trong chế độ xem chỉnh sửa. Tôi thực sự giấu nó với một wrapper (display:none
) nhưng tôi đã tự hỏi nếu có một cách tốt hơn để làm điều này. Mã của tôi trông giống như sau:Làm thế nào để vô hiệu hóa một trường trong chế độ xem chỉnh sửa bằng Symfony 2 FormBuilder
EntityType
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('fieldToDisabledInEditView');
// ...
EntityController
public function newAction() {
$entity = new Entity;
$form = $this->createForm(new EntityType, $entity);
// ...
}
public function editAction() {
$entity = new Entity;
$form = $this->createForm(new EntityType, $entity);
// ...
}
(cành lá) Mẫu mới
<form>
{{ form_row(form.fieldToDisabledInEditView) }}
{# ... #}
Chỉnh sửa (cành lá) Template
<form>
<span class="theValueOfTheHiddenField">{{ entity.fieldToDisabledInEditView }}</span>
<div style="display:none">
{{ form_row(form.fieldToDisabledInEditView) }}
</div>
{# ... #}
Tôi biết đây là một câu hỏi cũ nhưng nhận ra rằng màn hình hiển thị: none sẽ vẫn gửi giá trị ở dạng bị vô hiệu hóa sẽ không hiển thị. Vì vậy, bạn có thể xóa một số nội dung bạn không muốn. – shokora