php - Symfony2 ajax form validation render errors in twig -
php - Symfony2 ajax form validation render errors in twig -
i have form in symfony2 validate ajax. working, , json "global" (for global errors) , "fields" (errors each field in here) in success statement of ajax call:
{"global":[],"fields":{"fos_user_registration_form_username":"please come in username","fos_user_registration_form_email":"please come in email","fos_user_registration_form_plainpassword_first":"please come in password"}}
my question is: best way nowadays these errors "fields" in view each field? rendered view elements next when validated form without ajax:
<div class="form-group"> {{ form_label(form.xyz) }} {{ form_widget(form.xyz) }} {{ form_errors(form.xyz) }} </div>
how can inject errors "field" list of json object appropriate
{{ form_errors(form.xyz) }}
?
i love hear ideas , best practices.
regards.
you add together them via javascript/jquery.
i've similar situation , code:
the js code post form info , show errors messages in case of errors.
$.ajax({ url : $(this).attr('action') + ".json", method : 'post', info : $(this).serialize(), }).done(function(data) { // code in case of success }).fail(function(jqxhr) { $.each(jqxhr.responsejson.errors.children, function(k, v) { errorstext = ""; if ((v.errors) && (v.errors.length > 0)) { $.each(v.errors, function(n, errortext) { errorstext += errortext; }); $('#form_'+k).tooltip('destroy'); $('#form_'+k).tooltip({'title': errorstext}); $('#form_'+k).closest('div[class="form-group"]').addclass('has-error'); } else { $('#form_'+k).closest('div[class="form-group has-error"]').removeclass('has-error'); $('#form_'+k).tooltip('destroy'); } }); }
here json in case of error. standard 1 if utilize fosrestbundle
{ "code":400, "message":"validation failed", "errors":{ "children":{ "name":{ "errors":[ "this value should not blank." ] } "email":{ "errors":[ "this value should not blank." ] } "privacy":{ "errors":[ "this value should not blank." ] } } } }
the html form is
<form id="sfform" action="/landingbuilder/landing/test_template" method="post"> <div class="form-group"> <label class="control-label required" for="form_name">name</label> <input type="text" id="form_name" name="form[name]" required="required" class="form-control" /> </div> [..] </form>
if need more details ask
php jquery ajax forms symfony2
Comments
Post a Comment