我已经按照 Symfony文档开发了一个基本的用户注册表。尝试提交密码和失败登录计数不能为空的表单时,我收到验证错误:
data.password This value should not be null
data.failedLogins This value should not be null
RegistrationFormType 类:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstname', TextType::class, ['label'=> false, 'attr' => ['placeholder' => 'First name']])
->add('lastname', TextType::class, ['label'=> false, 'attr' => ['placeholder' => 'Last name']])
->add('email', EmailType::class, ['label'=> 'Email address', 'attr' => ['placeholder' => 'Email address']])
->add('plainPassword', PasswordType::class, [
'label' => 'Password',
'attr' => ['placeholder' => 'Must be at least 6 characters'],
'mapped' => false,
'constraints' => [
new NotBlank([
'message' => 'Please enter a password',
]),
new Length([
'min' => 6,
'minMessage' => 'Your password should be at least {{ limit }} characters',
// max length allowed by Symfony for security reasons
'max' => 4096,
]),
],
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
用户实体:
/**
* @var string The hashed password
* @ORM\Column(name="password", type="string", length=255, nullable=false, options={"fixed"=true})
*/
private $password;
/**
* @var int
*
* @ORM\Column(name="failed_logins", type="integer", nullable=false)
*/
private $failedLogins;
问题似乎是表单验证器返回 false,因为它需要 'password' 和 'failedLogins' 字段。如代码示例所示,我不想在表单中包含这些字段并在提交表单后处理它们...
如果我向表单构建器添加虚拟隐藏字段,则表单处理正常:
->add('password', HiddenType::class, ['data' => 'abc'])
->add('failedLogins', HiddenType::class, ['data' => 0])
然而,这似乎不是正确的方法。任何有关为什么在表单生成器中未包含这些字段时会验证这些字段的见解将非常感谢。
一只甜甜圈