David
David

Reputation: 568

How to customize form handling URL

I'm using forms within a Symfony 4 project. I use constraints, all is working fine.

Code looks like this :

$form = $this->createFormBuilder()->getForm();
$form->add('name', TextType::class, [
    'required' => false,
    'constraints' => [
        new Assert\NotBlank([
            'message' => 'Please enter your name' ]) ]]);

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
    // Do something
}

Question : is it possible to add an named anchor to the URL called back by Symfony when handling request ?

I need this in case of form validation failure to make the browser scroll to the form (the form is not on the top of the page, so the user have to manually rescroll to the form)...

Upvotes: 0

Views: 57

Answers (2)

balzacLeGeek
balzacLeGeek

Reputation: 815

Add 'action' propriety on your Twig side:

{{ form_start(form, {'action': path('customize_path'), 'method': 'POST'}) }}
{# your forms fields #}
{{ form_end(form) }}

Thanks,

Upvotes: 1

David
David

Reputation: 568

I have found a (the ?) solution. Just have to specify the action URL with the named anchor, like this :

$form = $this->createFormBuilder($formDatas)
    ->setAction('/my-url#formanchor')
    ->getForm();

Upvotes: 0

Related Questions