php API preEdit event values

php API preEdit event values

mommom Posts: 14Questions: 4Answers: 0

Hi,

I want to have a table with a calculable field - "subtotal" which is the product of two other fields. I need to have it explicitly written to the database, but the user cannot modify it manualy. In the frontend the field is present in the datatable only and not in the editor, and I'm using inline editing with submitting of full row.

According to the manual, I figured I could use the "preEdit" event in the backend API, where to give value of the "subtotal" field so that all fields will be written with one query. Assuming that "preEdit" fires after validation has passed and before the actual DB query. And this is the piece of code that I have:

    ->on( 'preEdit', function ( $editor, $values ) {
            $editor->field( 'subtotal' )->setValue( $values['quantity'] * $values['price_sgl'] );
    } )

However it does not work. Unlike what I expected, $values is not an array with all fields in the submitted row, but it is a string (?) containing the row ID. What am I doing wrong?

The "postEdit" event seems to contain all the fields values in its $values parameter, and as a last resort I can add an UPDATE query there but this means one extra query.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,823Questions: 1Answers: 10,129 Site admin
    Answer ✓

    Hi,

    What you have looks almost perfect - however, preEdit passes in three parameters:

    1. $editor - The Editor instance that triggered the event
    2. $id - ID of the row to be edited
    3. $values - The values submitted by the client-side

    It looks like you might be using the same parameter from preCreate which do get passed in with that format.

    The PHP event reference contains the list of parameters for each of the events.

    Regards,
    Allan

  • mommom Posts: 14Questions: 4Answers: 0

    Thank you for overseeing this Allan... don't know how I've mistaken the arguments list, probably just as you said.

This discussion has been closed.