{"id":1005,"date":"2017-05-09T21:39:32","date_gmt":"2017-05-10T02:39:32","guid":{"rendered":"http:\/\/xfloyd.net\/blog\/?p=1005"},"modified":"2017-05-09T23:02:55","modified_gmt":"2017-05-10T04:02:55","slug":"form-builder-with-ajax","status":"publish","type":"post","link":"https:\/\/xfloyd.net\/blog\/?p=1005","title":{"rendered":"Form Builder with Ajax"},"content":{"rendered":"<p>Simple code examples on how to use Symfony form builder with Ajax and jquery validators<\/p>\n<p><!--more--><br \/>\nSymfoyny part:<\/p>\n<pre><code ><br \/>\n...<br \/>\nuse Symfony\\Component\\Form\\Extension\\Core\\Type\\SubmitType;<br \/>\nuse Symfony\\Component\\Form\\FormInterface;<br \/>\n...<br \/>\n        $form->handleRequest($request);<\/p>\n<p>        if ($form->isSubmitted()) {<br \/>\n            if ($form->isValid()) {<br \/>\n                $em = $this->getDoctrine()->getManager();<br \/>\n                $em->persist($empl_pay);<br \/>\n                $em->flush();<\/p>\n<p>            }<br \/>\n            $errors = $this->getErrorsFromForm($form);<\/p>\n<p>            return new JsonResponse($errors);<br \/>\n        }<\/p>\n<p>...<\/p>\n<p>    private function getErrorsFromForm(FormInterface $form)<br \/>\n    {<br \/>\n        $errors = array();<br \/>\n        foreach ($form->getErrors() as $error) {<br \/>\n            $errors[] = $error->getMessage();<br \/>\n        }<br \/>\n        foreach ($form->all() as $childForm) {<br \/>\n            if ($childForm instanceof FormInterface) {<br \/>\n                if ($childErrors = $this->getErrorsFromForm($childForm)) {<br \/>\n                    $errors[$childForm->getName()] = $childErrors;<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        return $errors;<br \/>\n    }<\/p>\n<p><\/code><\/pre>\n<p>Recommended front end using JavaScript parsley:<\/p>\n<pre><code ><br \/>\n        var $form = $(\"form[name='form']\");<br \/>\n        $form.submit(function(e) {<br \/>\n            e.preventDefault();<br \/>\n            $form.parsley().reset();<br \/>\n            var $action = window.location.pathname;<br \/>\n            $.post($action, $(this).serialize(), function(data) {<br \/>\n                $.each( data, function( key, value ) {<br \/>\n                    console.log( 'ERROR ' + key + ': ' + value );<br \/>\n                    $('#form_' + key).parsley().addError('forcederror', {message: value, updateClass: true});<br \/>\n                });<br \/>\n            });<br \/>\n        });<br \/>\n<\/code><\/pre>\n<p>For jQuery validator use this:<\/p>\n<pre><code ><br \/>\n            errors = { personalid: \"Please enter an ID to check\" };<br \/>\n            $validator.showErrors(errors);<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Simple code examples on how to use Symfony form builder with Ajax and jquery validators<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,13],"tags":[],"_links":{"self":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1005"}],"collection":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1005"}],"version-history":[{"count":4,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1005\/revisions"}],"predecessor-version":[{"id":1010,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1005\/revisions\/1010"}],"wp:attachment":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}