form_errors不显示任何内容

时间:2018-05-02 19:28:48

标签: symfony

我很难从表单验证中显示错误。当我不正确填写表单时,不会显示任何内容。

这是我的控制器:

public function Register(Request $request){
    $user = new User();

    // Accounts must be enabled manually

    $authenticator = new GoogleAuthenticator();

    $user->setEnabled(false);
    $user->setAdmin(false);
    $user->setSecret($authenticator->createSecret());

    $form = $this->createFormBuilder($user)
        ->add('email', TextType::class)
        ->add('email', RepeatedType::class)
        ->add('password', PasswordType::class)
        ->add('password', RepeatedType::class)
        ->add('phone', TelType::class)
        ->add('secret', HiddenType::class)
        ->add('save', SubmitType::class, array('label' => 'Register'))
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // $form->getData() holds the submitted values
        // but, the original `$task` variable has also been updated
        $user = $form->getData();

        // ... perform some action, such as saving the task to the database
        // for example, if Task is a Doctrine entity, save it!
        // $entityManager = $this->getDoctrine()->getManager();
        // $entityManager->persist($task);
        // $entityManager->flush();

        return $this->redirectToRoute('register_success');
    }

    return $this->render('register.html.twig', array(
        'form' => $form->createView()
    ));
}

我的模板

{{ form_start(form) }}
    {{ form_errors(form) }}
    <div>
        {{ form_errors(form.email) }}
        {{ form_label(form.email) }} : 
        {{ form_widget(form.email.first, { 'attr' : {'placeholder' : 'Email address'}})}}
        {{ form_widget(form.email.second, { 'attr' : {'placeholder' : 'Email address (again)'}})}}
    </div>

    <div>
        {{ form_errors(form.password) }}
        {{ form_label(form.password) }} : 
        {{ form_widget(form.password.first, { 'attr' : {'placeholder' : 'Password'}})}}
        {{ form_widget(form.password.second, { 'attr' : {'placeholder' : 'Password (again)'}})}}
    </div>

    <div>
        {{ form_errors(form.phone) }}
        {{ form_label(form.phone) }} : 
        {{ form_widget(form.phone, { 'attr' : {'placeholder' : 'International format (+33)'}}) }}
    </div>

    <div>
        {{ form_widget(form.save) }}
    </div>

{{ form_end(form) }}

使用预先填充的数据重新加载表单但不显示错误(仅在源代码中为空行)

等待你的提示
感谢

2 个答案:

答案 0 :(得分:0)

需要设置验证器(以及错误消息)才能显示它们。

感谢您的帮助。

答案 1 :(得分:0)

首先,您无需“重复”已定义的 RepeatedType 字段,电子邮件和密码。

更改为:

$form = $this->createFormBuilder($user)
    ->add('email', RepeatedType::class, array(
         'type' => TextType::class,
         'invalid_message' => 'The email fields must match.',
         'options' => array('attr' => array('class' => 'email-field')),
         'required' => true,
         'first_options'  => array('label' => 'Email'),
         'second_options' => array('label' => 'Repeat Email'),
     ))
    ->add('password', RepeatedType::class, array(
         'type' => PasswordType::class,
         'invalid_message' => 'The password fields must match.',
         'options' => array('attr' => array('class' => 'password-field')),
         'required' => true,
         'first_options'  => array('label' => 'Password'),
         'second_options' => array('label' => 'Repeat Password'),
    ));

要显示错误,请使用:

{{ form_errors(form.email.first)}}
{{ form_errors(form.email.second)}}

{{ form_errors(form.password.first)}}
{{ form_errors(form.password.second)}}

参考

Symfony 4 RepeatedType field documentation