Hidden field in editor, need to submit this field on edit (inline) BUT NOT on create

Hidden field in editor, need to submit this field on edit (inline) BUT NOT on create

ASWSASWS Posts: 43Questions: 4Answers: 0
edited December 2022 in Editor
editor = new $.fn.dataTable.Editor({
      ajax: "/api/NewUser",
      table: "#xy",
      formOptions: {
          inline: {
          onBlur: true,
          submit: 'allIfChanged'
         }
         },
         fields: [
{ label: "Anrede", name: "user.gender", type: "radio", options: [{ label: "Herr", value: 1 }, { label: "Frau", value: 0 }], def: 1 },
{ label: "", name: "user.user_id", type: "hidden" },    ** <<<< this field not to be submitted at all on create, only on edit** 
{ label: "", name: "user.title", type: "hidden" }
{ label: "Vorname1", name: "user.given_name1" },
...
...

The field's value is always null or empty on create, therefore it should NOT be submitted on create, where it throws an error for being empty / having a wrong format (int).
On edit it works fine as is.
The fields value on create can not be set downhill on validation, it should simply not be submitted at all. (...not possible to just set a default value here, as the db table expects this field to be null or absent, on insert. Any integer default value set on this field before submitting e.g. collides with db constraints etc.)

Think there must be a quick and easy solution to this one.
Thanks for your help! :]
m

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,993Questions: 87Answers: 421
    edited December 2022 Answer ✓
    editor
        .on('preSubmit', function( e, d, action) {
            if ( typeof d.data !== 'undefined' ) {
                if ( action === 'create' ) {
                    var key = Object.keys(d.data)[0];
                    delete d.data[key].user.user_id;
                }
            }
        })  
    
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Excellent answer.

    Another option that might be worth considering is to have a second Editor instance - i.e. one for create, one for edit. Then setup the fields as you need for each.

    There is fields.submit which is currently just a boolean. You've got me wondering if I should make it capable of being a string of create, edit or both as well as the boolean...

    Allan

  • ASWSASWS Posts: 43Questions: 4Answers: 0

    rf1234
    I just copy pasted your solution and it worked perfect, this never ever happens... great, many thanks!! Perfect as is!

    I spend a couple of useless hours in the beginning on playing around using .add and .clear on preCreate etc. Then serverside on validation some creative but most of all unsuccessfull trial and error loss of time.

    I initially also tried the second 'editorCreate..' instance, @Allen, but it nearly doubles the code, nobody will understand why so much identical code for one micro difference, plus, both instances interfered because of my zero patience, overlooking something. I reverted to single instance as fast as I thought it migth be a plan B.

    So, rf1234's

    if ( action === 'create' ) {
    var key = Object.keys(d.data)[0];
    delete d.data[key].user.user_id;
    ...

    nails it. Amen.
    Thanks again both of you for your replies. :)
    m

Sign In or Register to comment.