Skip to Content
Menu
This question has been flagged
1 Reply
2029 Views

Hello guys !

I'm facing an issue trying to prevent the "apply" form from submitting... I'm trying to make a few other checks before actually sending it, but nothing that I try seems to be working, the form sends itself every time.


Here is what I'm trying :

$(document).ready(function() {
$('form').on('submit', function(e) {
e.preventDefault();
console.log('BOUH');
return false;
});
});

Do you have any idea how to solve this ?

Thank you very much !

Avatar
Discard
Best Answer

Hi Mariska, 

I've been in the same situation that you described.

The way I approach this, is not to attach an event listener to the submit form, but rather create a click event and attach it to the button which submits the form instead.

That said, you could try doing the following, in which the #button_id is the id of your submitting button.

```

$( "#button_id" ).click(function() {  
    // your validation logic goes here
    if (everything_is_ok()) {
          $( "#form_id" ).submit();
     }
     else {
       throw new Error("msg")  
    } });

```
Hope this helps :) 


Avatar
Discard
Author

Hello Dennis !
Thank you very much for your answer. It's a smart workaround, but unfortunately it doesn't work in my case.

Here is what I tried :

$('.validation_check').click(function(ev) {
$('form').submit();
});

This solution gave me a Bad Request because of csrf token. I then tried to add this in my form:

<input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>

Again, unfortunately no luck. When I click the button, I fall on a white page, and the form is not submitted.

Any clue what's happening here ?

Right...
I just did a couple research on Stack Overflow and stumbled on this helpful thread ( [link](https://stackoverflow.com/questions/5384712/intercept-a-form-submit-in-javascript-and-prevent-normal-submission) )
The theme among those answers is that you need to wait for the document or the window object to finish loading first, then move on to the clicking event.
```
$(document).ready(function() {
$('.validation_check').click(function(ev) {
$('form').submit();
});
}

Author

Thanks for the link.
I tried to wait for the load of the document but it didn't seem tobe the issue. In the mean time I went for another solution by heriting an odoo function and call super on it.

Thanks a lot for your time

Related Posts Replies Views Activity
5
Dec 24
6986
0
Dec 24
56
0
Dec 24
39
0
Oct 24
79
2
Sep 24
345