How to redirect to position on page after rendering form with errors





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















In symfony/twig I have a contact page that takes name and e-mail address that can be submitted. The input is validated. The form fields are at the bottom of the page. If the form is submitted I want to redirect to the bottom part of the page where the form is and where the errors or success message is displayed. In case of succes I managed to do this in the controller with;



if ($form->isValid()) {
.... code to send mail
return $this->redirect($request->getUri()."#form");
}


If the form is not valid the page is rendered like this:



        else {
$request->getSession()->getFlashBag()->add('error', 'Message not send!');
return $this->render('default/index.html.twig', array(
'form' => $form->createView(),
));
}


and if form is not valid it returns to the top of the page.
I want to return to the bottom of the page (url/#form) where the form part is and the errors are visible so the viewer sees the message was not send because of errors. Is it possible to add an anchor in rendering the form? (And not use javascript).










share|improve this question

























  • You should look here stackoverflow.com/questions/33525293/… (try to google before)

    – Pimento Web
    Jan 3 at 15:57











  • Have a look there stackoverflow.com/questions/5694244/…

    – Gregoire Ducharme
    Jan 3 at 15:57











  • The redirect with anchor #form works for form->isValid. But my question is how to get the anchor in rendering the page if the form is unvalid. Both suggested questions don't answer this.

    – zef
    Jan 3 at 16:04











  • <form action="#form" method="POST"> should solve that - To do this with symfony check here

    – DarkBee
    Jan 3 at 17:30











  • According to stackoverflow.com/a/53693081/1478128 what I want is not possible. The suggestion of @candybeer to use javascript was useful. I use javascript now with an event handler on submitting the form to add an anchor in the url. This works for both cases valid and unvalid form.

    – zef
    Jan 3 at 17:56


















0















In symfony/twig I have a contact page that takes name and e-mail address that can be submitted. The input is validated. The form fields are at the bottom of the page. If the form is submitted I want to redirect to the bottom part of the page where the form is and where the errors or success message is displayed. In case of succes I managed to do this in the controller with;



if ($form->isValid()) {
.... code to send mail
return $this->redirect($request->getUri()."#form");
}


If the form is not valid the page is rendered like this:



        else {
$request->getSession()->getFlashBag()->add('error', 'Message not send!');
return $this->render('default/index.html.twig', array(
'form' => $form->createView(),
));
}


and if form is not valid it returns to the top of the page.
I want to return to the bottom of the page (url/#form) where the form part is and the errors are visible so the viewer sees the message was not send because of errors. Is it possible to add an anchor in rendering the form? (And not use javascript).










share|improve this question

























  • You should look here stackoverflow.com/questions/33525293/… (try to google before)

    – Pimento Web
    Jan 3 at 15:57











  • Have a look there stackoverflow.com/questions/5694244/…

    – Gregoire Ducharme
    Jan 3 at 15:57











  • The redirect with anchor #form works for form->isValid. But my question is how to get the anchor in rendering the page if the form is unvalid. Both suggested questions don't answer this.

    – zef
    Jan 3 at 16:04











  • <form action="#form" method="POST"> should solve that - To do this with symfony check here

    – DarkBee
    Jan 3 at 17:30











  • According to stackoverflow.com/a/53693081/1478128 what I want is not possible. The suggestion of @candybeer to use javascript was useful. I use javascript now with an event handler on submitting the form to add an anchor in the url. This works for both cases valid and unvalid form.

    – zef
    Jan 3 at 17:56














0












0








0








In symfony/twig I have a contact page that takes name and e-mail address that can be submitted. The input is validated. The form fields are at the bottom of the page. If the form is submitted I want to redirect to the bottom part of the page where the form is and where the errors or success message is displayed. In case of succes I managed to do this in the controller with;



if ($form->isValid()) {
.... code to send mail
return $this->redirect($request->getUri()."#form");
}


If the form is not valid the page is rendered like this:



        else {
$request->getSession()->getFlashBag()->add('error', 'Message not send!');
return $this->render('default/index.html.twig', array(
'form' => $form->createView(),
));
}


and if form is not valid it returns to the top of the page.
I want to return to the bottom of the page (url/#form) where the form part is and the errors are visible so the viewer sees the message was not send because of errors. Is it possible to add an anchor in rendering the form? (And not use javascript).










share|improve this question
















In symfony/twig I have a contact page that takes name and e-mail address that can be submitted. The input is validated. The form fields are at the bottom of the page. If the form is submitted I want to redirect to the bottom part of the page where the form is and where the errors or success message is displayed. In case of succes I managed to do this in the controller with;



if ($form->isValid()) {
.... code to send mail
return $this->redirect($request->getUri()."#form");
}


If the form is not valid the page is rendered like this:



        else {
$request->getSession()->getFlashBag()->add('error', 'Message not send!');
return $this->render('default/index.html.twig', array(
'form' => $form->createView(),
));
}


and if form is not valid it returns to the top of the page.
I want to return to the bottom of the page (url/#form) where the form part is and the errors are visible so the viewer sees the message was not send because of errors. Is it possible to add an anchor in rendering the form? (And not use javascript).







php symfony validation redirect twig






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 16:09







zef

















asked Jan 3 at 15:54









zefzef

3811316




3811316













  • You should look here stackoverflow.com/questions/33525293/… (try to google before)

    – Pimento Web
    Jan 3 at 15:57











  • Have a look there stackoverflow.com/questions/5694244/…

    – Gregoire Ducharme
    Jan 3 at 15:57











  • The redirect with anchor #form works for form->isValid. But my question is how to get the anchor in rendering the page if the form is unvalid. Both suggested questions don't answer this.

    – zef
    Jan 3 at 16:04











  • <form action="#form" method="POST"> should solve that - To do this with symfony check here

    – DarkBee
    Jan 3 at 17:30











  • According to stackoverflow.com/a/53693081/1478128 what I want is not possible. The suggestion of @candybeer to use javascript was useful. I use javascript now with an event handler on submitting the form to add an anchor in the url. This works for both cases valid and unvalid form.

    – zef
    Jan 3 at 17:56



















  • You should look here stackoverflow.com/questions/33525293/… (try to google before)

    – Pimento Web
    Jan 3 at 15:57











  • Have a look there stackoverflow.com/questions/5694244/…

    – Gregoire Ducharme
    Jan 3 at 15:57











  • The redirect with anchor #form works for form->isValid. But my question is how to get the anchor in rendering the page if the form is unvalid. Both suggested questions don't answer this.

    – zef
    Jan 3 at 16:04











  • <form action="#form" method="POST"> should solve that - To do this with symfony check here

    – DarkBee
    Jan 3 at 17:30











  • According to stackoverflow.com/a/53693081/1478128 what I want is not possible. The suggestion of @candybeer to use javascript was useful. I use javascript now with an event handler on submitting the form to add an anchor in the url. This works for both cases valid and unvalid form.

    – zef
    Jan 3 at 17:56

















You should look here stackoverflow.com/questions/33525293/… (try to google before)

– Pimento Web
Jan 3 at 15:57





You should look here stackoverflow.com/questions/33525293/… (try to google before)

– Pimento Web
Jan 3 at 15:57













Have a look there stackoverflow.com/questions/5694244/…

– Gregoire Ducharme
Jan 3 at 15:57





Have a look there stackoverflow.com/questions/5694244/…

– Gregoire Ducharme
Jan 3 at 15:57













The redirect with anchor #form works for form->isValid. But my question is how to get the anchor in rendering the page if the form is unvalid. Both suggested questions don't answer this.

– zef
Jan 3 at 16:04





The redirect with anchor #form works for form->isValid. But my question is how to get the anchor in rendering the page if the form is unvalid. Both suggested questions don't answer this.

– zef
Jan 3 at 16:04













<form action="#form" method="POST"> should solve that - To do this with symfony check here

– DarkBee
Jan 3 at 17:30





<form action="#form" method="POST"> should solve that - To do this with symfony check here

– DarkBee
Jan 3 at 17:30













According to stackoverflow.com/a/53693081/1478128 what I want is not possible. The suggestion of @candybeer to use javascript was useful. I use javascript now with an event handler on submitting the form to add an anchor in the url. This works for both cases valid and unvalid form.

– zef
Jan 3 at 17:56





According to stackoverflow.com/a/53693081/1478128 what I want is not possible. The suggestion of @candybeer to use javascript was useful. I use javascript now with an event handler on submitting the form to add an anchor in the url. This works for both cases valid and unvalid form.

– zef
Jan 3 at 17:56












1 Answer
1






active

oldest

votes


















0














if you use redirectToUrl so you will lose the form data, so you can do this with simple jquery code:



{% if not form.vars.valid %}
<script>
$(document).ready(function () {
$('html,body').animate({
scrollTop: $('#form').offset().top
}, 1000);
});
</script>
{% endif %}





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025680%2fhow-to-redirect-to-position-on-page-after-rendering-form-with-errors%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    if you use redirectToUrl so you will lose the form data, so you can do this with simple jquery code:



    {% if not form.vars.valid %}
    <script>
    $(document).ready(function () {
    $('html,body').animate({
    scrollTop: $('#form').offset().top
    }, 1000);
    });
    </script>
    {% endif %}





    share|improve this answer




























      0














      if you use redirectToUrl so you will lose the form data, so you can do this with simple jquery code:



      {% if not form.vars.valid %}
      <script>
      $(document).ready(function () {
      $('html,body').animate({
      scrollTop: $('#form').offset().top
      }, 1000);
      });
      </script>
      {% endif %}





      share|improve this answer


























        0












        0








        0







        if you use redirectToUrl so you will lose the form data, so you can do this with simple jquery code:



        {% if not form.vars.valid %}
        <script>
        $(document).ready(function () {
        $('html,body').animate({
        scrollTop: $('#form').offset().top
        }, 1000);
        });
        </script>
        {% endif %}





        share|improve this answer













        if you use redirectToUrl so you will lose the form data, so you can do this with simple jquery code:



        {% if not form.vars.valid %}
        <script>
        $(document).ready(function () {
        $('html,body').animate({
        scrollTop: $('#form').offset().top
        }, 1000);
        });
        </script>
        {% endif %}






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 21:40









        houshous

        9561340




        9561340
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025680%2fhow-to-redirect-to-position-on-page-after-rendering-form-with-errors%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory