php - 解析“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']
解决方案
你应该这样做:
$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) }}
推荐阅读
- python - 1.7 LAB:通过 normalizing_zybook 调整列表中的值
- python - 使用子进程读取 adb shell 但我没有看到从 adb shell 返回的任何数据
- vba - 如何将组添加到 ListView?
- javascript - 如何为 dompdf Laravel 制作动画进度
- php - 从链接表中获取选中的复选框
- flutter - 启动画面 - 在 initState 上完成异步调用之前,AnimationController 不会启动
- css - 停止 Dashlane 对特定输入字段的自动填充?
- javascript - 如何使用材质 UI 选项卡的道具更改选项卡?
- vuetify.js - v-switch 的点击事件不一致
- c++ - 将带有非类型参数的模板适配为模板模板参数