Novalidate不适用于FOS用户注册表格

阿尔伯茨基

我改写了FOS UserBundle登记表并添加默认选项:“ATTR” =>数组作为回答(“NOVALIDATE” =>“NOVALIDATE”)这里(这似乎是正确的方式去),但由于一些奇怪的原因,novalidated IS在表格后而不是表格后添加到div中。

表格类型:

<?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中。难道我做错了什么?

zilongqiu

novalidate="novalide"div是错误的。您需要将此放置在表单上。

例如使用控制器

$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()。

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

重要的:

如果您使用FOSUserBundleconfigureOptions则不能直接在form标记上应用,因为该标记是在捆绑包视图中手动调用的。

中的示例registration_content.html.twig

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章