dimanche 1 mars 2015

Use same form on two different pages (URLs) in Symfony 2

I am using the FOSUserBundle with Symfony 2.5. I have overridden the default registration form and defined a new URL for it.



fos_user_registration_register:
path: /user/new.html
defaults: { _controller: FOSUserBundle:Registration:register }


and this is the form



class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->remove('plainPassword');
$builder->remove('username');
$builder->remove("email");

$builder->add('terms', 'checkbox', array('required' => true,
'mapped' => false,
'constraints' => new True(array('message' => 'Required!'))));

$builder->add("email", "email", array("required" => true, "mapped" => true, "constraints" => new MailBlacklist()));
}

public function getParent()
{
return 'fos_user_registration';
}

public function getName()
{
return 'my_user_registration';
}
}


finally I registered it as a service.


When I show the form on the URL /user/new.html and submit it, it works: validation errors are shown and if everything is fine with the data, the user is created.


But when I show exactly the same form on a different URL (the root) and submit the form, no validation is done and the user is not saved. Instead, I get redirected to /user/new.html and see an empty form.


I create the form like this



$form = $this->createForm('my_user_registration')->createView();


Why isn't this working? Is FOSUserBundle checking the referer or something?


My template



<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST">
{{ form_widget(form) }}
<div>
<input type="submit" value="Registrieren" />
</div>
</form>


Would be great if someone could help my with this "little" problem :)


Aucun commentaire:

Enregistrer un commentaire