Using postCreate and postEdit together

Using postCreate and postEdit together

kkutikkuti Posts: 13Questions: 5Answers: 0

I am able to use both postCreate and postEdit individually successfully. When used together only the postEdit works. I am trying to create table entries of the user and the date when a records are created and when they are edited, when the Add button is used then postCreate is triggered and when the edit button is clicked the postEdit is triggered. The example given works fine because the postEdit only updates one field, in mine I need two fields updated. Please help

Replies

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    edited May 2019

    Hi @kkuti ,

    Those events, postCreate and postEdit, are called after the data has been submitted to the server, so if you want those values stored in the server it would be better to use preSubmit.

    That said, I've set up a test case here and the events are triggering as expected. Could you look at that, please, and see if it helps. If it's still not working for you, please can you update my example, or link to your page, so that we can see the problem.

    Cheers,

    Colin

  • kkutikkuti Posts: 13Questions: 5Answers: 0

    Thanx Colin for your prompt reply. I will paste my code and perhaps you can advise me. Code is between the dotted lines

    Field::inst( 'created_by' )->set( Field::SET_EDIT ),
        Field::inst( 'created_date' )->set( Field::SET_EDIT ),
        Field::inst( 'created_by' )->set( Field::SET_CREATE ),
        Field::inst( 'created_date' )->set( Field::SET_CREATE )
    )
    ->on( 'preEdit', function ( $editor, $values ) {
        $editor
            ->field( 'created_by' )
            ->setValue( $_SESSION['userlog'] );
     } )
     ->on( 'preEdit', function ( $editor, $values ) {
        $editor
            ->field( 'created_date' )
            ->setValue( date('c') );
     } )
    
     ->on( 'preCreate', function( $editor, $values ) {
        $editor
            ->field( 'created_by' )
            ->setValue( $_SESSION['userlog'] );
     } )
     ->on( 'postCreate', function( $editor, $id, $values, $row ) {
        $editor
           ->field( 'created_date' )
            ->setValue( date('c') );
     } )
    
    ->process( $_POST )
    

    json();">json();" href="#----->json();"> ->json();

    What I am trying to achieve here is that the two fields created_by and created_date are populated depending on whether a record is been created or updated.
    Please how can I achieve that

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin

    Colin thought you were talking about the client-side events. I see from your code above it was actually the server-side PHP events you were considering.

    Field->setValue() won't be useful in postCreate or postEdit - the write has already taken place to the database, so the change wouldn't make any difference.

    Why not just put both setValue calls into the pre* event(s)?

    I'm not quite clear what you want to happen to created_date on edit? ->set(false) can be used to not write to it.

    Allan

  • kkutikkuti Posts: 13Questions: 5Answers: 0

    Good evening Allan, you have helped me resolve this. It is now working as intended.It was to have two entries created_by and created_date inserted into the table when either a new record was created or an record was edited.

This discussion has been closed.