Novalidate不适用于FOS用户注册表格 [英] Novalidate isn't working on FOS user registration form

查看:104
本文介绍了Novalidate不适用于FOS用户注册表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我改写了FOS UserBundle注册表并添加了默认选项:'attr'=> array('novalidate'=>'novalidate')作为回答

I overwrote the FOS UserBundle registration form and added the default options: 'attr'=> array('novalidate'=>'novalidate') as answered here (which seems like the right way to go) but for some strange reason the novalidated is added to a div right after the form instead of the form.

FormType:

<?php

namespace AppBundle\Form\Type;

use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use AppBundle\Services\RolesHelper;

class UserType extends BaseType
{
  /**
   * @var RolesHelper
   */
  private $roles;

  /**
   * @param string $class The User class name
   * @param RolesHelper $roles Array or roles.
   */
  public function __construct($class, RolesHelper $roles)
  {
    parent::__construct($class);

    $this->roles = $roles;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
    parent::buildForm($builder, $options);

    $builder->add('firstName')
            ->add('lastName')
            ->add('roles', 'choice', array(
              'choices' => $this->roles->getRoles(),
              'required' => false,
              'multiple'=>true
            ));
  }

  /**
   * {@inheritdoc}
   */
  public function getName()
  {
    return 'user_registration';
  }

  /**
   * @param OptionsResolverInterface $resolver
   */
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
    parent::setDefaultOptions($resolver);

    $resolver->setDefaults(array(
      'attr'=> array('novalidate'=>'novalidate'),
    ));
  }
}

这是我的表单的外观:

<form action="/app_dev.php/profile/edit" method="POST" class="fos_user_profile_edit">
    <div id="fos_user_profile_form" novalidate="novalidate">
    // ....
    </div>
</form>

为什么要将其添加到div而不是form元素之后的div中.我在做错什么吗?

Why would it be adding it to the div after the form instead of the form element. Am I doing something wrong?

推荐答案

div上的novalidate="novalide"错误.您需要将其放置在表单上.

The novalidate="novalide" on the div is wrong. You need to place this on the form.

例如使用控制器这样的示例

$form = $this->createForm(new TaskType(), $task, array(
    'attr' => array(
           'novalidate' => 'novalidate'
    )
));

或直接在视图中

{{ form_start(form, {attr: {novalidate: 'novalidate'}}) }}

最终结果

<form action="/app_dev.php/profile/edit" method="POST" class="fos_user_profile_edit" novalidate="novalidate">
    <div id="fos_user_profile_form">
    // ....
    </div>
</form>

通过表单(针对Symfony< = 2.6)的最佳解决方案

  /**
   * @param OptionsResolverInterface $resolver
   */
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
    $resolver->setDefaults(array(
      'attr'=> array('novalidate'=>'novalidate'),
    ));
  }

通过表单获得的最佳解决方案(对于Symfony> = 2.7)

configureOptions()方法是Symfony 2.7中引入的. 以前,该方法称为setDefaultOptions().

The configureOptions() method was introduced in Symfony 2.7. Previously, the method was called setDefaultOptions().

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'attr'=> array('novalidate'=>'novalidate'),
    ));
}

重要提示:

如果您使用的是 FOSUserBundle ,则configureOptions不能直接应用于form标记,因为此标记是在捆绑包视图中手动调用的.

If you're using FOSUserBundle, the configureOptions can't be applied directly on the form tag because this tag is manually called in the bundle views.

registration_content.html.twig :

<form action="{{ path('fos_user_resetting_reset', {'token': token}) }}" {{ form_enctype(form) }} method="POST" class="fos_user_resetting_reset">

这篇关于Novalidate不适用于FOS用户注册表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆