Global validator PHP

Global validator PHP

GargiucnGargiucn Posts: 104Questions: 29Answers: 0

I am referring to the discussion:
https://datatables.net/forums/discussion/43608
I would like to understand why I can't get the id of the row while I can get the value of the other fields...

$editor = Editor::inst( $db, 'forndoc', 'fod_id' );
$editor->fields(
    Field::inst( 'forndoc.fod_id' )->set( false ),
    Field::inst( 'forndoc.fod_idfor' ),
    Field::inst( 'forndoc.fod_iddoc' )
);

$editor->validator( function ( $editor, $action, $data ) {
    if ( $action === Editor::ACTION_EDIT ) {    
        foreach ( $data['data'] as $pkey => $values ) {
            $doc_id = $values['forndoc']['fod_iddoc'];
            $fod_idfor = $values['forndoc']['fod_idfor'];
            $fod_id = $values['forndoc']['fod_id']; // generate error
        }
     )
} );

I get the error message "Undefined index: fod_id" on an edit attempt.
As Gordonc200 asked then:
Is there a way (there must be) to extract the the ID of the row being edited?
Thanks,

Replies

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406
    edited April 2020

    A validator only validates what is being sent from the client.

    I keep making this mistake every once in a while: A front end editor instance ONLY sends those fields back to the server that are defined in it. So if you retrieve something but forget to define it in the front end editor it doesn't get sent back!

    Use something like this please:

    }, {
     type: "hidden",
     name: 'forndoc.fod_id'
    }, {
    

    @allan, I think this is counter intuitive: people are looking at the back end editor instance; they think they have everything available in the validator but they don't. I know this behavior is psycho-logical and not logical but it is a constant source of trouble for me - and not only for me. I keep forgetting the stuff I wrote above - just because it is counter intuitive. Can this be "fixed" (I know it is not a bug, but it would help if this doesn't happen any longer. Why don't all fields that are retrieved get sent back as the default?!)

  • Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

    Hi @allan. I would also just like to say that I agree with @rf1234, on the point that I also find it problematic that not all data is available server side in the validator and that there doesn't seem to be an easy way to access it. For example, if the $id was available, that would help greatly. Sometimes I don't want to define all the fields in the front end editor but I might still need to check things server side.

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406
    edited April 2020

    ok, but there is one thing I didn't mention ... This won't help "on create": The id field doesn't exist at the time the validator validates the data sent from the client because it is usually an auto-increment field. But you could still pass in $id as NULL for example in a create situation.

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

    I would like to understand why I can't get the id of the row while I can get the value of the other fields...

    I don't quite understand this original question. The primary key is in $pkey (although you'll need to chop of the row_ part (or other prefix if you've changed it).

    not all data is available server side in the validator and that there doesn't seem to be an easy way to access it

    The client-side Editor has two options:

    1. It can send only the data that was changed, or
    2. It can send all data it knows about (using the submit parameter).

    On the server-side, you can query the database if the client-side hasn't sent all the data you need, or you need to make sure you are reading the latest value.

    Why don't all fields that are retrieved get sent back as the default?!)

    Because there were enough requests from others that didn't want all values to be sent, when only one had been updated. As a compromise I put in the submit parameter to selectively send all.

    I'm not actually 100% clear on what the issue is here I'm afraid. Not getting the primary key for the row being edited? As noted, that is in the data submitted (it has to be, otherwise the database couldn't be updated correctly).

    Allan

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

    True and that is the issue: There is no submit option
    "submit all that had been retrieved from the server Editor instance (regardless of their existence in the client Editor instance)"

    Currently there are these submit options:

    This should be amended with a 4th option
    "allRetrieved": Submit all field values retrieved from the server, even if there are no changes.

    I know this option is difficult for "create" but in that case you could return NULL for the fields that are not defined in the client side Editor.

  • GargiucnGargiucn Posts: 104Questions: 29Answers: 0

    Damn, I often forget to define the fields that I will need in the editor ...
    Thanks for your help.
    Hope that the whole community of datatables is well and can serenely overcome this bad moment ...

This discussion has been closed.