Symfony:4 属性路径中给出的“整数”、“NULL”类型的预期参数

Kir*_*ran 0 symfony symfony4

低于错误,

属性路径“experience”中给出的“整数”、“NULL”类型的预期参数。

代码如下,

->add('experience', TextType::class, [
    'constraints' => [
        new NotBlank([
            "message" => $this->translator->trans('Please enter experience.')
        ]),
        new Length([
            'max' => 2
        ]),
    ],
    'required' => false
])
Run Code Online (Sandbox Code Playgroud)

当我提交空白表格(创建时间)时,它显示“请输入经验”。

现在在编辑表单上,如果我插入错误的值(更多数据,如 3434)它显示我的值不应超过 2 个字符

当我在编辑时发送空白数据时出现问题。提交空白经验(表格中的必填字段),它给出以下错误

属性路径“experience”中给出的“整数”、“NULL”类型的预期参数。

我曾尝试过互联网上的不同选项,例如将“empty_data”设置为 null 或 '' 但它不起作用。

emi*_*mix 7

null如果你接受值,你的 setter 方法应该允许值,根据形式判断:

public function setExperience(?int $experience): void
{
    $this->experience = (int) $experience;
}
Run Code Online (Sandbox Code Playgroud)