【问题标题】:An error has occurred resolving the options of the form "Symfony\Component\Form\Extension\Core\Type\PasswordType": The options "0", "1" do not exist解析“Symfony\Component\Form\Extension\Core\Type\PasswordType”形式的选项时发生错误:选项“0”、“1”不存在
【发布时间】:2020-07-14 06:28:49
【问题描述】:

刚开始学习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"、 “帮助”、“help_attr”、“help_html”、“help_translation_parameters”、“inherit_data”、“invalid_message”、“invalid_message_parameters”、“is_empty_callback”、“label”、“label_attr”、“label_format”、“label_html”、“label_translation_parameters” "、"映射"、"方法"、"post_max_size_message"、"property_path"、"required"、"row_attr"、"translation_domain"、"trim"、"upload_max_size_message"、"validation_groups"。

我什至没有在任何地方设置 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' =>'确认密码']

【问题讨论】:

  • 您是否错过了解析器,或者您没有将其添加到您的问题中?
  • 这里没加,我发现哪里出错了---- 'first_options' => ['label' => 'Password'], 'second_options' => ['label' =>'确认密码']

标签: php symfony


【解决方案1】:

你应该这样做:

        $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) }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    相关资源
    最近更新 更多