防止Symfony的CheckboxType铸造字符串到Bool
我正在使用Symfony 5.4 REST API工作,并使用FormTypes验证了用户的输入。我有一个必需的布尔字段,如果我收到该字段的任何非树树输入,则希望抛出一个异常。但是,Symfony的CheckBoxType会自动将任何非树树输入设置为true
当我提交表单时,因此我无法检查我的输入是否是合法的布尔值。
我的代码看起来像这样:
$inputModel= new InputModel();
$this->form->setData($inputModel);
if ($this->request->isMethod('PUT')) {
// Here the vaue of $this->request->request->all()['myBooleanField'] is "asdf"
$this->form->submit($this->request->request->all());
// Here the value of inputModel->getMyBooleanField() is true
//...
}
我的FormType buildform
方法看起来像这样:
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
//...
->add('myBooleanField', typeForm\CheckboxType::class, [ 'required' => true ])
//...
;
}
任何帮助都将受到赞赏!
I am working on a Symfony 5.4 REST API and validating a user's input using FormTypes. I have a required boolean field and would like to throw a form exception if I receive any non-boolean input for that field. However, Symfony's CheckboxType automatically sets any non-boolean input to true
when I submit the form, so I can not check if my input was a legit boolean.
My code looks like this:
$inputModel= new InputModel();
$this->form->setData($inputModel);
if ($this->request->isMethod('PUT')) {
// Here the vaue of $this->request->request->all()['myBooleanField'] is "asdf"
$this->form->submit($this->request->request->all());
// Here the value of inputModel->getMyBooleanField() is true
//...
}
My FormType buildForm
method looks like this:
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
//...
->add('myBooleanField', typeForm\CheckboxType::class, [ 'required' => true ])
//...
;
}
Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论