1

I'm trying to create a good validation js <=> Symfony2, I came up with the collection of all the errors and creating a path to id form elements and everything looks it is working properly, including relationships and collections forms, my question is whether you can do something nicer? Symfony can have a bundle for that? which will be much more useful than my piece of code?

//in controller

        $ajax = $request->request->get('ajax');
        if($form->isSubmitted() && $form->isValid() && !$ajax){

        } elseif($form->isSubmitted() && !$form->isValid() && $ajax){
            $serializer = $this->get('serializer');
            return new JsonResponse(array(
                'valid' => false,
                'errors' => $serializer->serialize(
                    $form->getErrors(true),
                    'json')

            ));
        }

// and in js

$('form[name="product"]').on('submit',function(){
    var data = $(this).serialize();
    data = data + '&ajax=true';
    $.ajax({
            method: "POST",
            url: Routing.generate('shop_add_products', { slug: $(this).data('slug') }),
            data: data
        })
        .done(function( msg ) {
            //there i must write check if errors then not return false and allow normall process form
            $('input,textarea,select').parent().removeClass('has-error').find('span.help-block').remove();;
            var errors = JSON.parse(msg.errors);
            var filteredErrors = [];
            var mappedErrors = [];
            for(var k in errors){
                filteredErrors[errors[k].messageTemplate] = errors[k].message;
                var selector;
                var path = errors[k].cause.propertyPath.split('.');
                if(path.length == 2){
                    selector = 'product_' + path[1];
                } else {
                    var path1 = path[0].replace('children[','');
                    path1 = path1.replace(']','');
                    var path2 = path[1].replace('data[','');
                    path2 = path2.replace(']','');
                    selector = 'product_' + path1 + '_' + path2 + '_' + path[2];
                }
                var helper = '<span class="help-block">' + errors[k].message + '</span>';
                $('#' + selector).parent().addClass('has-error').append(helper);
            }
        });
    return false;
});

How to valid files sending via ajax and mapping it to this function? See there

3
  • What about the Validator already available in Symfony : symfony.com/doc/2.7/validation.html Commented Oct 30, 2016 at 23:51
  • 1
    Yes i use annotations as validation but my ask is how to create a good validation without "refreshing" the page. I build my code that sends form to the controller and if has errors return json response with array of errors then i fetch this array and map errors into form fields that works fine but i ask for better way to do that Commented Oct 30, 2016 at 23:56
  • Oh yeah sorry, it seems obvious now. I haven't try it though : github.com/formapro/JsFormValidatorBundle Commented Oct 31, 2016 at 0:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.