editor pre create function

editor pre create function

eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0
edited May 2020 in Editor

i need to create new row in table called service, and return the id of this row, and set the value of this row in other field in other table called CruisesService

this is my code:

editor.PreCreate += (sender, e) => {
                    object value;
                    e.Values.TryGetValue("Service.Service_Name_Heb", out value);
                    var id = CreateService(Enums.ServiceType.CruisesService, (string)value);
                    editor.Field("CruisesService.CruisesService_id").SetValue(id);
                };

my question, is why precreate called even if there are error in validation?
what event i can used to make it happen clean without problem

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    my question, is why precreate called even if there are error in validation?
    what event i can used to make it happen clean without problem

    You could use "submitSuccess" and check whether the action was "create".

    https://editor.datatables.net/reference/event/submitSuccess

  • eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0

    hey, thank for replay, i want to make it on server side not client

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    https://editor.datatables.net/manual/php/events

    just pick an event that suits you

  • eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0

    i know which events have in server side, i pick the pre-create because it must done before new row created, but it seems to skip validtion

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited May 2020

    You seem to be using node.js. I didn't recognize that. At least for the PHP event I can tell you that it doesn't skip validation.

  • eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0

    hey Allan, if you can look on the problem and have any idea how to solve this, it will be excellent for me

  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin

    my question, is why precreate called even if there are error in validation?

    preCreate is called before validation so validation can be added by that event handler! It's a good point, we should maybe have another event handler in between the two - postValidation or something...

    Allan

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited May 2020

    @allan
    That's a big loophole in my validation! Please fix this asap.

    I didn't understand the garbage in my database ... Now I do. Could you just pls change preCreate with a hotfix. No additional event needed in my opinion :)

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394

    I agree with @rf1234. It seems more logical to me that preCreate should be run against validated data.

  • eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0

    hey Allan,do you have other way to solve the problem now? until there will be another event? or it will take couple of days to add new events?

  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin

    I can't change when preCreate is triggered since folks depend on the current behaviour and it is documented that you can use it to assign values, which then need to be validated.

    But I will add a new event that will address this. It will be in Editor 1.9.3 which I expect to drop soon. I'll post back with the git commits so you can use it pre-release if you want.

    Allan

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited May 2020

    preCreate is called before validation so validation can be added by that event handler!

    Ok @allan , but I need a hot fix. How can I call all of the field validators and the global validator inside preCreate right now?

    I can't change when preCreate is triggered since folks depend on the current behaviour and it is documented that you can use it to assign values, which then need to be validated.

    Since assigning values is done programatically the values should be valid anyway. For me validation only applies to user entries.

    I assume preEdit has the same problem? Is that right?

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    @eyal_hb
    I am checking my code right now - and found many problems because of the missing validation. I found one very special case where I really do validation INSIDE the preEdit event handler.

    I can only provide a PHP example. So you would need to try to find this for node.js by yourself. The problem is: You need to sort of repeat the validation you have in your field instance validation already which for me is not feasible at all ...

    ->on('preCreate', function ( $editor, $values ) {
       .....
       $errorCondition = true;
        ....
        if ( $errorCondition ) {
            $editor
            ->field('yourField')
            ->validator( function () {
                return 'this field is in error';
            } );
        }
    } )
    
  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin
    Answer ✓

    Here we go - I've added validatedCreate and validatedEdit:

    Thanks for the feedback on this!

    Allan

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    :) good news! Thank you!

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    Just added those two lines to the Editor code and tested it with PHP. Works like a charm. Thanks for the hot fix, Allan!

    Roland

  • eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0

    hey Allan,thank you for the update,last question is how i implement this in net? i have dll of the DataTables-Editor-Server

  • gordonc200gordonc200 Posts: 39Questions: 7Answers: 0

    Just a heads up. Should these validatedCreate and validatedEdit maybe be documented here https://editor.datatables.net/manual/events and here https://editor.datatables.net/reference/event/ ? I only happened upon them with a google search and they are clearly useful!

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited October 2020

    Those are the javascript events ... But validatedCreate and validatedEdit are server side and you can find the docs here (for PHP):

    https://editor.datatables.net/manual/php/events

  • gordonc200gordonc200 Posts: 39Questions: 7Answers: 0

    Thanks rf1234. Schoolboy error.

This discussion has been closed.