首页 > 解决方案 > 解析“Symfony\Component\Form\Extension\Core\Type\PasswordType”形式的选项时发生错误:选项“0”、“1”不存在

问题描述

刚开始学习symfony框架,报错。

解析“Symfony\Component\Form\Extension\Core\Type\PasswordType”形式的选项时发生错误:选项“0”、“1”不存在。定义的选项有:“action”、“allow_extra_fields”、“allow_file_upload”、“always_empty”、“attr”、“attr_translation_parameters”、“auto_initialize”、“block_name”、“block_prefix”、“by_reference”、“compound”、“constraints” "、"csrf_field_name"、"csrf_message"、"csrf_protection"、"csrf_token_id"、"csrf_token_manager"、"data"、"data_class"、"已禁用"、"empty_data"、"error_bubbling"、"error_mapping"、"extra_fields_message"、 “帮助”,

我什至没有在任何地方设置 0 或 1。尝试访问https://127.0.0.1:8000/register时发生错误。我的代码注册控制器是

    <?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Routing\Annotation\Route;

class RegistrationController extends AbstractController
{
    /**
     * @Route("/register", name="register")
     */
    public function register()
    {

        $form = $this->createFormBuilder()
            ->add('username')
            ->add('password', RepeatedType::class,[
                'type' => PasswordType::class,
                'required' => true,
                'first_options' => ['label','Password'],
                'second_options' => ['label','Confirm Password']
            ])
            ->getForm();
        return $this->render('registration/index.html.twig', [
            'form' => $form->createView()
        ]);
    }
}

我一直在 youtube 上关注 symfony 教程,但他没有收到错误消息。我能做些什么?谢谢

编辑 我在'first_options'和'second_options'中有一个错误正确的方法是'first_options' => ['label' => 'Password'],'second_options' => ['label' =>'Confirm Password']

标签: phpsymfony

解决方案


你应该这样做:

        $form = $this->createFormBuilder()
        ->add('username', TextType::class)
        ->add('password', RepeatedType::class, [
            'type' => PasswordType::class,
            'invalid_message' => 'The password fields must match.',
            'options' => ['attr' => ['class' => 'password-field']],
            'required' => true,
            'first_options'  => ['label' => 'Password'],
            'second_options' => ['label' => 'Confirm Password'],
        ])
        ->getForm();

并认为:

    {{ form_start(form) }}
        {{ form_row(form.username) }}
        {{ form_row(form.password.first) }}
        {{ form_row(form.password.second) }}
        <button type="submit">Register!</button>
    {{ form_end(form) }}

推荐阅读