在Symfony 2/3中有条件地启用表单类型



我有两种表格类型,显示2选择

$builder
->add('control1', EntityType::class, [
'label' => 'Country',
'class' => Country::class,
])
->add('control2', EntityType::class, [
'label' => 'State',
'class' => State::class,
])

然后在树枝模板中

{{ form_row(form.control1)}}
{{ form_row(form.control2)}}

只有在控件1中选择了特定选项时,我才能启用控件2?谢谢

已提交表单的动态生成

您希望自定义特定于用户提交的数据的表单。例如,假设你有一张体育集会的登记表。某些活动将允许您指定您在赛场上的首选位置。例如,这将是一个选择字段。然而,可能的选择将取决于每项运动。您需要正确的选项才能通过验证。

以下示例创建了一个包含两个字段countrystate的表单。state字段取决于country字段,如果未选择任何国家/地区,则该字段将保持禁用状态,没有可用的选择。

表单类型

namespace AppBundleForm;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentFormFormInterface;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormEvents;
use SymfonyComponentFormFormEvent;
use DoctrineORMEntityManagerInterface;
use AppBundleEntityCountry;
use AppBundleEntityState;
class CountryStateType extends AbstractType
{
private $entityManager;
private $countryRepository;
private $stateRepository;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager     = $entityManager;
$this->countryRepository = $entityManager->getRepository(Country::class);
$this->stateRepository   = $entityManager->getRepository(State::class);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('country', EntityType::class, [
'class' => Country::class,
'placeholder' => 'Select',
'choice_label' => 'name',
'label' => 'Country'
]);
$formModifier = function (FormInterface $form, $countryId = null) {
$country  = null === $countryId ? null : $this->countryRepository->find($countryId);
$states   = null === $country ? [] : $this->stateRepository->findAll();
$disabled = null === $country ? true : false;
$form->add('state', EntityType::class, [
'class' => State::class,
'placeholder' => 'Select',
'choice_label' => 'name',
'label' => 'State',
'choices' => $states,
'disabled' => $disabled
]);
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
$data = $event->getData();
$data = null === $data ? [] : $data;
$countryId = array_key_exists('country', $data) ? $data['country'] : null;
$formModifier($event->getForm(), $countryId);
}
);
$builder->get('country')
->addEventListener(FormEvents::POST_SUBMIT, 
function (FormEvent $event) use ($formModifier) {
$country = $event->getForm()->getData();
$countryId = null === $country ? null : $country->getId();
$formModifier($event->getForm()->getParent() , $countryId);
}
);
}
}

模板

{{ form_start(form) }}         
{{ form_widget(form.country) }}
{{ form_widget(form.state) }}
<button type="submit">Submit</button>
{{ form_end(form) }}
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
var $country = $('#{{ form.country.vars.id }}');
$country.change(function () {
var $form = $(this).closest('form');
var data = {};
data[$country.attr('name')] = $country.val();
$.ajax({
url: $form.attr('action'),
type: $form.attr('method'),
data: data,
success: function (html) {
$('#{{ form.state.vars.id }}').replaceWith(
$(html).find('#{{ form.state.vars.id }}')
);
}
});
});
</script>

相关内容

  • 没有找到相关文章

最新更新