0

I've a form and I want to submit it from an link. Something like :

<form action="{{ path('update', { 'id': entity.id }) }}" id="update" method="post" {{ form_enctype(edit_form) }}>
    {{ form_widget(edit_form) }}
</form>

... some HTML

<a href="javascript:$('#update').submit();">Valid</a>

But when I submit the form using this link, the form is not validated in Javascript (check empty fields etc...) and don't display error tooltips

If I create a button in the form tag, it works, but i want this button outside of the form tag.

How can I do this ?

edit : My form is in a modal dialog.

Gilles
  • 317
  • 5
  • 15

3 Answers3

0

not an ideal answer, but i guess this should work ...

have a submitbutton in your form,hide it using css

then where your current js link is, make it so that this uses jquery to simulate a click on the correct form-submit-button

Again, not ideal, but i guess it should do the job..

Sam Janssens
  • 1,491
  • 1
  • 12
  • 30
0

This will work as you expected,

<script language="javascript">    
    $.fn.submitForm = function() {
        $('#update').submit();
    };    
</script>

and the call should be,

<a href="javascript:$(this).submitForm();">Valid</a>

But it is not appreciated to use javascript executions within href, look at this previous discussion on this topic,

Then what i'm wondering is, since you are using Jquery why you are looking to trigger the submit within html href? , simply in a javascript file say script.js

//assuming there is only one <a></a> tag
$(document).on('click', 'a', function(){
   $('#update').submit();
});
Community
  • 1
  • 1
code-jaff
  • 9,230
  • 4
  • 35
  • 56
0

I've done it another way.

To have the HTML5 field control, I have put an hidden submit button and I did;

$('#my_submit_button').click();

and it works.

Alfred
  • 21,058
  • 61
  • 167
  • 249
Gilles
  • 317
  • 5
  • 15