->add('useExtendedCustomer', CheckboxType::class, [
'label' => 'Create New Customer ?',
'mapped' => false,
'required' => false,
])
->add('customer', CustomerAutocompleteChoiceType::class, [
'label' => 'Search Customer by Last Name',
'resource' => 'sylius.customer',
'choice_name' => 'lastName',
'choice_value' => 'id',
])
->add('customer', CustomerType::class);
;
这是我的“客户”字段,我想根据用户是否单击“useExtendedCustomer”复选框来显示该字段。
如果用户单击复选框(“useExtendedCustomer”),那么我想显示以下内容:
->add('customer', CustomerType::class);
因此删除它(因为它首先是默认显示):
->add('customer', CustomerAutocompleteChoiceType::class, [
label' => 'Search Customer by Last Name',
'resource' => 'sylius.customer',
'choice_name' => 'lastName',
choice_value' => 'id',
])
树枝:
{{ form_start(form, {'action': path(configuration.vars.route.name|default(configuration.getRouteName('create')), configuration.vars.route.parameters|default({})), 'attr': {'class': 'ui loadable form', 'novalidate': 'novalidate'}}) }}
<div class="ui two column stackable grid">
<div class="column">
<div class="ui segment">
{{ form_row(form.customer) }}
<div id="licence_customer" style="display: none;">
<div class="two fields">
{# {{ form_row(form.customer.firstName) }}
{{ form_row(form.customer.lastName) }} #}
</div>
</div>
{{ form_row(form.useExtendedCustomer)}}
{# <h4 class="ui dividing header">Customer details</h4>
<div class="two fields">
{{ form_row(form.customer.firstName) }}
{{ form_row(form.customer.lastName) }}
</div>
{{ form_row(form.customer.email) }}
{{ form_row(form.customer.group) }}
</div>
<div class="ui segment">
<h4 class="ui dividing header">Extra information</h4>
{{ form_row(form.customer.gender) }}
{{ form_row(form.customer.birthday) }}
{{ form_row(form.customer.phoneNumber) }}
{{ form_row(form.customer.subscribedToNewsletter) }}
</div>
</div> #}
目前,由于我无法两次显示相同的“客户”字段,因此有一个从以下位置开始的第二个注释表单:
{{ form_row(form.customer.firstName) }}
我想根据单击复选框来显示。
所以逻辑是这样的:
默认显示如下:
->add('customer', CustomerAutocompleteChoiceType::class, [
label' => 'Search Customer by Last Name',
'resource' => 'sylius.customer',
'choice_name' => 'lastName',
choice_value' => 'id',
])
这个在树枝上:
{{ form_row(form.customer) }}
如果复选框为真,则在上面的行中添加以下内容:
->add('customer', CustomerAutocompleteChoiceType::class, [
label' => 'Search Customer by Last Name',
resource' => 'sylius.customer',
choice_name' => 'lastName',
choice_value' => 'id',
])
并在树枝中显示评论部分。
这里有一个问题我不知道如何动态解决?
您有一个依赖字段的情况。我建议您遵循 Ryan Weaver 在 SymfonyCasts 上提供的教程:https://symfonycasts.com/screencast/symfony-forms/choice-type-fields。它的所有代码都在https://github.com/SymfonyCasts/dynamic-forms。