Home > Back-end >  How to define if a form adds new entry or updates an existing one?
How to define if a form adds new entry or updates an existing one?

Time:11-25

I need to set different JS event on "submit" event of the form. The form generated by twig template is:

{{ form_start(theForm)}}
    {{ form_row(theForm.name) }}
    {{ form_row(theForm.lastName) }}
...
{{ form_end(theForm) }}

In JS file:

$('form').on('submit', function(e) {
   if (new) {
   //do some staff
   } else {
   //do the other staff
   }
});

How to define if user adds new entry or updates existing one in this form?

CodePudding user response:

You can check if the form has an id:

$('form').on('submit', function(e) {
   let new = {{ form.vars.value.id }}; 
   //if creation, new will be null; else will have the id of existing
  
   ...
});

But it is recommended to have two twig files, one for new the other for update

  • Related