1

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?

Community
  • 1
  • 1
albertski
  • 2,428
  • 1
  • 25
  • 44

1 Answers1

1

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">
zilongqiu
  • 846
  • 5
  • 12
  • since this is a form created with FOS UserBundle I can't run the createForm method. Any way I can add the novalidate in my class? – albertski Jul 15 '15 at 15:33
  • Sorry i was editing my post. You can just apply it directly via the view. – zilongqiu Jul 15 '15 at 15:34
  • 1
    @zilongqui I would need to overwrite the template file to do this. I would leave that as my last resort. Are you sure there isn't a way I can handle this via setDefaultOptions() or something like that? – albertski Jul 16 '15 at 03:18
  • @albertski +1 you can define the `novalidation` using `setDefaultOptions` (for Symfony version under 2.7) and `configureOptions` (for version >= 2.7) – zilongqiu Jul 16 '15 at 03:40
  • Unfortunately configureOptions() adds the novalidate to the div after the form. I have a feeling that the reason this is happening because of the template. https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Profile/edit_content.html.twig – albertski Jul 16 '15 at 13:30
  • At this time I don't have a view form but am using the FOS's template to display the form: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Profile/edit_content.html.twig – albertski Jul 16 '15 at 13:59
  • I'm wondering if the best way to do this is to copy the template (https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Profile/edit_content.html.twig) to my bundle using these instructions: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_templates.md and adding the novalidate attribute manually: – albertski Jul 16 '15 at 14:11
  • @albertski I tried in **Symfony2.6** with `setDefaultOptions` and it worked. This problem only exist in **Symfony2.7**, the `configureOptions` doesn't add our custom attributes (here `novalidate`) in the `form tag`. I'm still searching. xd – zilongqiu Jul 16 '15 at 15:32
  • @albertski Sorry i was so silly !! As you mentioned in this [doc](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Profile/edit_content.html.twig), the `form tag` was write manually. So the `configureOptions` couldn't be applied for this tag. You have to override the FOSUserBundle view. – zilongqiu Jul 17 '15 at 11:00
  • Thanks that was it. Kind of sucks that they did it that way. – albertski Jul 21 '15 at 19:02