use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Validator\Constraints\EqualTo;
// ...
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
if (isset($data['cityA'])) {
$form->add('cityB', TextType::class, [
'constraints' => [
new EqualTo([
'value' => $data['cityA'],
'message' => 'Must be the same as cityA',
]),
],
]);
}
// Here other constraints
});
1条答案
按热度按时间9vw9lbht1#
这里有一个解决方案来实现你想要做的事情:
在FormType中,在PRE_SUBMIT上添加一个事件侦听器,并像这样添加约束:
字符串