Conditional Global Validator (PHP)

Conditional Global Validator (PHP)

gordonc200gordonc200 Posts: 39Questions: 7Answers: 0

I'd like to disable editing or deletion of a record if a certain field is already set in that record.

Looking at the manual here I think this should be possible. I'm thinking of passing in the record's ID to the validation function and then running a sql query to see if the field in question is already set for that record.

However taking the example from the manual

// Create and edit with dependent validation
Editor::inst( $db, 'table' )
    ->fields( ... )
    ->validator( function ( $editor, $action, $data ) {
        if ( $action === Editor::ACTION_CREATE || $action === Editor::ACTION_EDIT ) {
            foreach ( $data['data'] as $pkey => $values ) {
                if ( $values['country'] === 'US' && $values['location'] === 'London UK' ) {
                    return 'London UK is not in the US';
                }
            }
        }
    } )
    ->process( $_POST )
    ->json();

if I try to pass in data from the field "tablename.idfieldname" as $values['tablename.idfieldname] I get the error message "Undefined index: tablename.idfieldname" on an edit attempt.

I think this is because the value "idfieldname" isn't being edited and so isn't being passed in the data. Thus it is undefined. That's if I'm understanding this correctly.

Is there a way (there must be) to extract the the ID of the row being edited?

Many thanks in advance to anyone with the answer!

Answers

  • tangerinetangerine Posts: 3,348Questions: 36Answers: 394

    Editor has a built-in "unique" validator.
    https://editor.datatables.net/manual/php/validation#Database

  • gordonc200gordonc200 Posts: 39Questions: 7Answers: 0

    So even though that's in the "field" validation section I can still use the validate class in a global validation to check if the column value is null and if it isn't the edit shouldn't run?

    Sounds good. Will try tomorrow. Thanks.

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    $values['tablename.idfieldname]

    Should be references as $values['tablename']['idfieldname] in PHP - its a nested array.

    Editor's unique validator will operate on a single field only. I'm not quite clear from your description if that is what you want or not?

    Allan

  • gordonc200gordonc200 Posts: 39Questions: 7Answers: 0

    Hi Allan

    I have a function completely separate from datatables which sends emails based on data from a given database table row. Datatables creates, edits and deletes these rows.

    The emailing function foreign to datatables writes a date value to the row in question in column "email_sent" when an email is sent based on that row.

    I'd like it if editing or deletion is disabled once a value has been set in this row. I'm not that concerned if datatables shows the row as editable or deletable in the front end (although it would be nice if it did) but would like it if the php prevented the editing or deletion if this particular value is set.

    If there's a way of telling datatables the row in question has that value set without a separate sql query in a custom validation function then even better but if that function is required I need to look up the correct row. The problem I have is working out how to extract the row identifier.

    Thanks
    Gordon

  • tangerinetangerine Posts: 3,348Questions: 36Answers: 394

    My post on "unique" validation is not relevant here, Gordon - sorry. I must have misread your original post.
    Are you successfully retrieving row-by-ro data now, and if so what does it look like?

  • gordonc200gordonc200 Posts: 39Questions: 7Answers: 0

    Hi Tangerine. No worries! I haven't made any progress on this yet I'm afraid. I'm sure an idea will come up though.

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    I think the preEdit (and preRemove) events are the ones you want here in that case. They are cancellable by simply returning false from the event handler.

    So you can perform whatever check you need to in that function (including a database lookup if needed, or just checking the values sent by the client-side) and then either return false or true depending on if you want the row to be edited/deleted or not.

    The client-side interaction that triggers this action won't see any change (i.e. you can still request an edit / delete), but if the action is cancelled, nothing will change on the client-side.

    Allan

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

    I read this because that thread https://datatables.net/forums/discussion/61265/global-validator-php#latest references this one.

    Looks like the same problem: An id field is being retrieved in the back end editor instance (or not even that: we can't see it from the code posted) and then isn't sent back from the client. It isn't available in the validator of course. But read the other thread ...

    Don't forget the syntax error in the first post above that @allan pointed out.

This discussion has been closed.