Symfony仅阅读复选框检查

发布于 2025-02-14 01:29:34 字数 412 浏览 0 评论 0原文

我正在寻找一种禁用文本输入验证的方法,并在检查框时阅读文本类型?

$formBuilder
            ->add('text',      TextType::class,array(
            'required' => true,
            'constraints' => array(
                new NotBlank()
            )))
            ->add('box',      CheckboxType::class, array(
                'mapped' => false,
                'label' => 'Box'
            ))

    ;

I'm looking for a way to disable text input validation and readOnly the TextType if the box is checked?

$formBuilder
            ->add('text',      TextType::class,array(
            'required' => true,
            'constraints' => array(
                new NotBlank()
            )))
            ->add('box',      CheckboxType::class, array(
                'mapped' => false,
                'label' => 'Box'
            ))

    ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流心雨 2025-02-21 01:29:34

您需要将EventListener添加到您的FormBuilder,并基于输入数据添加约束。阅读有关动态形式修改的更多信息在这里

未经测试,但也许是这样的:

$formBuilder->add('text', ...);
$formBuilder->add('box', ...);

$formBuilder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
   $data = $event->getData();
   $form = $event->getForm();

   if (isset($data["box"])) {  // Or use something like $data->getBox() if you are using objects
      $formBuilder->add('text', ...); // Replace field without constraints
   }
   
});

请记住,这只会进行服务器端验证,而不是前端。

You'll need to add an eventListener to your formBuilder and add constraints based on input data. Read more about dynamic form modification here.

Not tested but maybe something like this:

$formBuilder->add('text', ...);
$formBuilder->add('box', ...);

$formBuilder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
   $data = $event->getData();
   $form = $event->getForm();

   if (isset($data["box"])) {  // Or use something like $data->getBox() if you are using objects
      $formBuilder->add('text', ...); // Replace field without constraints
   }
   
});

Keep in mind that this will only do server side validation, not frontend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文