if empty field does not save it

if empty field does not save it

buroticaburotica Posts: 7Questions: 3Answers: 0

Hello,
I made a profile manager with a datatable editor.
There are several fields, including the password. I would like the password field to be saved only if the field is not empty.
My code :

<?php

/*
 * Editor server script for DB table client
 * Created by http://editor.datatables.net/generator
 */

// DataTables PHP library and database connection
include( "lib/DataTables.php" );

// Alias Editor classes so they are easy to uses
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;


// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'users', 'id' )
    ->fields(
        Field::inst( 'users.login' ),
        Field::inst( 'users.password' )
            ->get( false ) // never read from the db
            ->setFormatter( function ( $val, $data ) {
                return md5( $val );
        } ),
        Field::inst( 'profiles.name' ),
        Field::inst( 'users.profile' )
            ->options( Options::inst()
                ->table( 'profiles' )
                ->value( 'id' )
                ->label( 'name' )
            ),
        Field::inst( 'users.reference' )
            ->validator( Validate::notEmpty() ),
        Field::inst( 'users.firstname' ),
        Field::inst( 'users.lastname' ),
        Field::inst( 'users.company' ),
        Field::inst( 'users.phone' ),
        Field::inst( 'users.mail' )
            ->validator( Validate::email() )
    )
    ->leftJoin( 'profiles', 'profiles.id', '=', 'users.profile' )
    ->where( $key = 'users.profile', $value = '1', $op = '!=' )
    ->on( 'preEdit', function ( $editor, $id, $values ) {
        if ( $values['users.password'] === '' ) {
            $editor
                ->field( 'users.password' )
                ->set( false );
        }
    } )
    ->process( $_POST )
    ->json();

Best regard
Arnaud

Answers

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Hi Arnaud,

    A server-side event (specifically preEdit) is the answer here. Have a look at this part of the post introducing the server-side events in Editor.

    Allan

  • buroticaburotica Posts: 7Questions: 3Answers: 0

    I use the preEdit, but it does not work.

    ->on( 'preEdit', function ( $editor, $id, $values ) {
        if ( $values['users.password'] === '' ) {
            $editor->field( 'users.password' )->set( false );
        }
    } )
    
    
  • buroticaburotica Posts: 7Questions: 3Answers: 0

    It's good, it's solved.

        ->on( 'preEdit', function ( $editor, $id, $values ) {
            if ( $values['users']['password'] === '' ) {
                $editor->field( 'users.password' )->set( false );
            }
        } )
    
This discussion has been closed.