Novalidate在FOS用户注册表上不起作用[英] Novalidate isn't working on FOS user registration form

本文是小编为大家收集整理的关于Novalidate在FOS用户注册表上不起作用的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我覆盖了fos userBundle注册表格并添加了默认选项:'attr'=> array('novalidate'=>'novalidate'),at noce there (似乎是正确的方法),但是出于某种奇怪的原因,NovaliDated在表格之后添加到了一个div,而不是该表格而不是形式.

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而不是表单元素.我做错了吗?

推荐答案

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

例如,使用控制器

这样
$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)

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

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

重要:

如果您正在使用 FOSUserBundle ,configureOptions无法直接应用于form标签,因为此标签是在捆绑图中手动调用的.

a>:

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

本文地址:https://www.itbaoku.cn/post/2090797.html

问题描述

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'),
    ));
  }
}

This is how my form looks:

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

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

推荐答案

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

For example like this using the controller

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

Or directly in the view

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

Final result

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

EDIT:

Best solution via the form (for Symfony <= 2.6) WORKS

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

Best solution via the form (for Symfony >= 2.7)

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'),
    ));
}

IMPORTANT:

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.

Example in the registration_content.html.twig :

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