gpt4 book ai didi

validation - 将验证器添加到自定义表单类型

转载 作者:行者123 更新时间:2023-12-04 08:49:11 27 4
gpt4 key购买 nike

如何将验证器添加到 自定义表单类型 ?以 IBAN 为例。

use Symfony\Component\Validator\Constraints\IbanValidator;

class IBANType extends AbstractType
{
public function getName()
{
return 'iban';
}

public function getParent()
{
return 'text';
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
//IbanValidator ???
}
}

Here他们建议使用监听器:

The interface FormValidatorInterface was deprecated and will be removed in Symfony 2.3.

If you implemented custom validators using this interface, you can substitute them by event listeners listening to the FormEvents::POST_BIND (or any other of the *BIND events). In case you used the CallbackValidator class, you should now pass the callback directly to addEventListener.



但是怎么样?

最佳答案

尝试这个:

use Symfony\Component\Validator\Constraints\IbanValidator;

class IBANType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('iban', 'text', [
'constraints' => [
new IbanValidator(),
],
]);
}
}

关于validation - 将验证器添加到自定义表单类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33166944/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com