Symfony 5.1.7-无法从禁用的选择框选项类型保存数据
我想在不更改选择框信息的情况下编辑表单。我设法禁用了选择框,但当我试图保存表单时,出现了一个错误 属性路径“action”处应提供类型为“string”和“null”的参数 表单可以与Create New配合使用(因为已启用操作选项类型)。在编辑期间,我禁用了action ChoiceType,因为我不希望用户更改它 如何从禁用的输入中保存数据 _form.html.twigSymfony 5.1.7-无法从禁用的选择框选项类型保存数据,symfony,Symfony,我想在不更改选择框信息的情况下编辑表单。我设法禁用了选择框,但当我试图保存表单时,出现了一个错误 属性路径“action”处应提供类型为“string”和“null”的参数 表单可以与Create New配合使用(因为已启用操作选项类型)。在编辑期间,我禁用了action ChoiceType,因为我不希望用户更改它 如何从禁用的输入中保存数据 _form.html.twig {{ form_start(form) }} {{ form_errors(form) }}
{{ form_start(form) }}
{{ form_errors(form) }}
{% if Update_req %}
{{ form_row(form.action, {'attr': {'disabled': true}}) }}
{% else %}
{{ form_row(form.action) }}
{% endif %}
{{ form_row(form.date) }}
{{ form_row(form.amount) }}
{{ form_row(form.remark) }}
<button class="btn btn-success">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}
{% block body %}
{{ include('tithe/_form.html.twig', {'button_label': 'Update', 'Update_req': true}) }}
<hr />
<a href="{{ path('tithe_index', {'bid': balance_id}) }}" class="btn btn-secondary">back to list</a>
{{ include('tithe/_delete_form.html.twig') }}
{% endblock %}
{% extends 'base.html.twig' %}
{% block title %}Edit{% endblock %}
{% block body %}
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_row(form.action, {'attr': {'readonly': true}}) }}
{{ form_row(form.date) }}
{{ form_row(form.amount) }}
{{ form_row(form.remark) }}
<button class="btn btn-success">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}
{% endblock %}
TitheType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('action', ChoiceType::class, [
'required' => true,
'label' => 'Action',
'multiple' => false,
'expanded' => false,
'choices' => [
'Withdrawal' => 'Withdrawal',
'Deposit' => 'Deposit',
],
])
->add('date', DateType::class, [
'years' => range(date('Y') - 1, date('Y'))
])
->add('amount')
->add('remark');
}
正如@Noman所说,禁用字段不是post 也许在编辑中,您不应该将“操作”绑定到表单并将其显示为单个标签。这样,实体中的值就不会发生任何变化 或者,您可以将“action”的前一个值保存在一个值中,然后将其添加到if子句中,这更脏:
public function edit(Request $request, Tithe $tithe, BalanceRepository $balanceRepository, SessionInterface $session): Response
{
$action = $tithe->getAction();
...
if ($form->isSubmitted() && $form->isValid()) {
$tithe->setAction($action);
...
}
...
}
这是我的解决办法。根据Alexandre第一命题,我创建了一个新表单来编辑信息。 我将“操作”选项类型更改为文本类型,作为只读文本
public function change(Request $request, TitheRepository $titheRepo, BalanceRepository $balanceRepository, SessionInterface $session, $id)
{
$bid = $session->get('BID');
$balance = $balanceRepository->find($bid);
$tithe = $titheRepo->find($id);
$form = $this->createFormBuilder()
->add('action', TextType::class)
->add('date', DateType::class)
->add('amount', TextType::class)
->add('remark', TextType::class)
->getForm();
$form['action']->setData($tithe->getAction());
$form['date']->setData($tithe->getDate());
$form['amount']->setData($tithe->getAmount());
$form['remark']->setData($tithe->getRemark());
$form->handleRequest($request);
...
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$tithe->setAction($data['action']);
$tithe->setDate($data['date']);
$tithe->setAmount($data['amount']);
$tithe->setRemark($data['remark']);
...
} // if ($form->isSubmitted() && $form->isValid())
return $this->render('tithe/change.html.twig', [
'form' => $form->createView(),
]);
}
和change.html.twig
{{ form_start(form) }}
{{ form_errors(form) }}
{% if Update_req %}
{{ form_row(form.action, {'attr': {'disabled': true}}) }}
{% else %}
{{ form_row(form.action) }}
{% endif %}
{{ form_row(form.date) }}
{{ form_row(form.amount) }}
{{ form_row(form.remark) }}
<button class="btn btn-success">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}
{% block body %}
{{ include('tithe/_form.html.twig', {'button_label': 'Update', 'Update_req': true}) }}
<hr />
<a href="{{ path('tithe_index', {'bid': balance_id}) }}" class="btn btn-secondary">back to list</a>
{{ include('tithe/_delete_form.html.twig') }}
{% endblock %}
{% extends 'base.html.twig' %}
{% block title %}Edit{% endblock %}
{% block body %}
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_row(form.action, {'attr': {'readonly': true}}) }}
{{ form_row(form.date) }}
{{ form_row(form.amount) }}
{{ form_row(form.remark) }}
<button class="btn btn-success">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}
{% endblock %}
{%extends'base.html.twig%}
{%block title%}编辑{%endblock%}
{%block body%}
{{form_start(form)}}
{{form_errors(form)}}
{{form_行(form.action,{'attr':{'readonly':true}}}
{{form_row(form.date)}
{{form_行(form.amount)}
{{form_row(form.remark)}
{{button_label}默认('Save')}
{{form_end(form)}}
{%endblock%}
禁用字段不会发布,请改用只读
。@Norman。我使用文本框,但不使用选择框。用户仍然可以更改选择框的项目。是否使用select2?@Norman。正如您在TitheType.php文件中看到的那样,我正在使用ChoiceType类的形式;不起作用。对于第一个解决方案,我如何将值(即ChoiceType)分配给新的文本框“action”。您不必为此创建其他表单,您可以将“$tithe”发送到视图,只显示“action”值。此外,您需要在表单中添加一个选项,以在表单中添加或不添加“操作”字段。