0

I'm working with Symfony2 and have a form which has two submit actions:

  1. on a submit within the form
  2. a javascript submit()

In the first option the HTML5 form validation runs smoothly, but not in the second option, and the aplication send the parameters without show the tooltips. What is the reason for this?

Controller:

public function showCombosAction(Request $request){

    $user = $this->getUser();
    $form = $this->createForm(new InfoFormType(), NULL, array('foo' => $user->getRoles()[0]));

    $form->handleRequest($request);

    if ($form->isValid()) {

        $params = $form->getData();

        if (!isset($params['tienda1'])) $params['tienda1'] = $user->getStore()->getId();

        $db = $this->get("db_connection_service");

        $store = array();
        if (isset($params['tienda2'])) {
            //Comparamos tienda con tienda
            $params['tienda2'] = $params['tienda2']->getId();
            $store = $db->findDataStoreVsStore($params['tienda1'], $params['tienda2'], 4);

        }else{
            echo $params['tienda1'];
            switch ($params['comparativa']){
                case 'anterior':
                    break;
                case 'totnacional':
                    $store = $db->findDataStoreVsNacional($params['tienda1'], 4);
                    break;
                case 'totregion':
                    $store = $db->findDataStoreVsRegion($params['tienda1'], 4, $user->getStore()->getRegion());
                    break;
                default:
                    break;
            }
        }
        $this->showInfoTwoStoresAction($params['tipo'], $store[0], $store[1]);
    }

    return $this->render('AppBundle:Admin:showcombos.html.twig', array(
            'form'   => $form->createView()
    ));

}

View:

{% extends "AppBundle::layout.html.twig" %}

{% block fos_user_content %}
<form action="{{ path('frontend_show_combos') }}" method="POST" class="form-     horizontal" name="combos">
{{ form_widget(form) }}
<div id="combos_tienda2"></div>
<input type="submit">
{{ form_end(form) }}


<script>
$(document).ready(function(){
    var $comparativa = $('#combos_comparativa');

    $comparativa.change(function() {
        var $form = $(this).closest('form');
        var data = {};
        data[$comparativa.attr('name')] = $comparativa.val();
        if ($comparativa.val() == "otra"){
            $.ajax({
                url : $form.attr('action'),
                type: $form.attr('method'),
                data : data,
                success: function(html) {
                    $('#combos_tienda2').replaceWith(
                            $(html).find('#combos_tienda2')
                        )
                    $('#combos_tienda2').change(function(){
                        $form.submit();
                        }
                    );
                }
            });
        }else{
            document.combos.submit();
        }

   });
});
 </script>
 {% endblock fos_user_content%}

Thank You in advance.

cmaciasg
  • 71
  • 2
  • 11
  • The HTML5 validation is not run because you are not actually submitting any form, just running some ajax when a field changes. You should have an actual form and submit it when the field changes. Then on the onSubmit method you can make your ajax call (also call event.preventDefault() to prevent the form being submited "normally") – Carlos Granados Oct 19 '15 at 15:55
  • found this article: http://stackoverflow.com/questions/11866910/how-to-force-a-html5-form-validation-without-submitting-it-via-jquery – Frank B Oct 19 '15 at 15:59
  • Thx for your response, Carlos, but I submit the form inside and out the ajax block "form.submit()" and "document.combos.submit()" In fact, I received that parameters. – cmaciasg Oct 19 '15 at 16:01

0 Answers0