Custom INSERT and field validation

Custom INSERT and field validation

cr1st1cr1st1 Posts: 14Questions: 5Answers: 0

I try to replace the default behavior of dataTables Editor like this :

        $data = Editor::inst( $DT, 'users', 'id' )
            ->fields(
                Field::inst( 'email' )
                    ->validator( Validate::email(
                        ValidateOptions::inst()
                        ->allowEmpty( false )
                        ->optional( false )
                        ->message( 'Provide a valid email' )
                    ))
            )

            ->on( 'preCreate', function ( $editor, $values ) {
                    ...
                    // here i place my custom code
                    ...
                    return false; // cancel the default behavior of Editor
            });

The problem is : the validation is not performed.
I would like to use the dataTables Editor validation functions and my custom code for record creation.
How can i do this ?

Replies

  • allanallan Posts: 63,179Questions: 1Answers: 10,410 Site admin

    You more or less can't I'm afraid. If you are doing your own insert / update / delete, then don't use the Editor libraries at all.

    You could pull in the validation classes if you wanted and use them, but the libraries aren't designed to operate the way you have it there (for example that doesn't just cancel Editor's insert, but it tells the client-side the insert didn't happen!).

    Allan

  • allanallan Posts: 63,179Questions: 1Answers: 10,410 Site admin

    Also worth noting that preCreate occurs before validation so that the event handler could potentially add validation if required.

    Allan

  • cr1st1cr1st1 Posts: 14Questions: 5Answers: 0

    Thanks @allan ! I will write my own code for this situation

This discussion has been closed.