Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony用户注册表验证错误_Php_Symfony_Symfony4_Symfony Forms - Fatal编程技术网

Php Symfony用户注册表验证错误

Php Symfony用户注册表验证错误,php,symfony,symfony4,symfony-forms,Php,Symfony,Symfony4,Symfony Forms,我已经按照Symfony开发了一个基本的用户注册表。我在尝试提交密码和失败登录计数不能为空的表单时收到验证错误: data.password This value should not be null data.failedLogins This value should not be null 注册FormType类: public function buildForm(FormBuilderInterface $builder, array $options) {

我已经按照Symfony开发了一个基本的用户注册表。我在尝试提交密码和失败登录计数不能为空的表单时收到验证错误:

data.password       This value should not be null
data.failedLogins   This value should not be null
注册FormType类:

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;
控制器:

public function register(Request $request, 
                         UserPasswordEncoderInterface $passwordEncoder, 
                         GuardAuthenticatorHandler $guardHandler, 
                         LoginFormAuthenticator $authenticator): Response
{
    $user = new User();
    $form = $this->createForm(RegistrationFormType::class, $user);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        $user->setPassword($passwordEncoder->encodePassword($user, $form->get('plainPassword')->getData()));
        $user->setfailedLogins(0);
问题似乎是表单验证程序返回false,因为它需要“password”和“failedLogins”字段。如代码示例所示,我不希望在表单中包含这些字段,并在表单提交后对其进行处理

如果我将虚拟隐藏字段添加到表单生成器中,则表单可以正常处理:

->add('password', HiddenType::class, ['data' => 'abc'])
->add('failedLogins', HiddenType::class, ['data' => 0])

然而,这似乎不是正确的方法。如果您能深入了解为什么这些字段在未包含在表单生成器中时会被验证,我们将不胜感激。

我相信您有两个不同的问题,这两个问题都是因为验证是针对与表单相关的实体进行的

从文档中:

Symfony,验证应用于基础对象(例如任务)。换句话说,问题不在于“表单”是否有效,而在于表单将提交的数据应用到$task对象之后,$task对象是否有效。调用$form->isValid()是询问$task对象是否具有有效数据的快捷方式


  • 您的密码在实体
    User
    中命名为
    password
    ,但在表单中命名为
    plainPassword
    。验证功能将在实体中找不到属性
    plainPassword
    ,并返回
    数据。password此数据不应为null
    。因此,您只需将表单字段名更改为
    password


  • 您的实体中有一个
    failedLogins
    ,但它在表单中不存在。默认情况下,表单验证检查所有实体的属性。由于
    failedLogins
    声明为
    nullable=false
    ,验证将返回
    数据。failedLogins此数据不应为null
    。在这种情况下,您需要允许您选择在表单验证中考虑
    User
    的哪些属性