Editor on preEdit with Condition

Editor on preEdit with Condition

vincmeistervincmeister Posts: 136Questions: 36Answers: 4

Hello Allan,

Is it possible editor on preEdit with Condition based on other field?

->on( 'preEdit', function ( $editor, $values ) {
            $editor
                ->field( 'act_log.log_status' )
                // condition here ->setValue(1);
            } )

what i want is:
if Field::inst( 'master_expedition.expedition_name' ), = a, then field( 'act_log.log_status' ) set to 1,
if Field::inst( 'master_expedition.expedition_name' ), = b, then field( 'act_log.log_status' ) set to 2

please advise, thank you

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Yes. You have the full set of values being given to you in the $values array, you would just access them here. For example if ( $values['master_expedition']['expedition_name'] === ... )

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    hello Allan,
    I got an Illegal string offset 'master_expedition' and Illegal string offset 'expedition_typewhen using this code, please advise, thank you

    ->on( 'preEdit', function ( $editor, $values ) {            
                if ( $values['master_expedition']['expedition_type'] === "Internal" ){ 
                    $editor
                        ->field( 'act_log.log_status' )
                        ->setValue (5);
                    }
                else {
                    $editor
                        ->field( 'act_log.log_status' )
                        ->setValue (6);
                    }
                } )
    
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Are you submitting that value from the client-side? What values are being submitted? The "Network" tab in your browser's console will show you.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    No,

    User only edit a act_log.outgoing_date

    then i want to setValue of act_log.log_status based on master_expedition.expedition_type

    if master_expedition.expedition_type = 'Internal' then act_log.log_status = 5
    else act_log.log_status = 6

    Please help, thank you

    Full Editor

    Editor::inst( $db, 'act_log' )
            ->field(
                Field::inst( 'act_log.incoming_date' )
                    ->getFormatter( function ( $val, $data, $opts ) {
                        if ($val === "0000-00-00 00:00:00"){
                            echo "";
                        }else{
                            return date( 'H:i', strtotime( $val ) );
                        }
                    } ),
                Field::inst( 'act_log.category' )
                    ->options( 'master_log_category', 'id', 'act_log_cat' ),
                Field::inst( 'master_log_category.act_log_cat' ),
                Field::inst( 'act_log.visitor_name' ),
                Field::inst( 'act_log.company_name' )
                    ->options( 'master_expedition', 'expedition_code', 'expedition_name' ),
                Field::inst( 'master_expedition.expedition_name' ),
                Field::inst( 'master_expedition.expedition_type' ),
                Field::inst( 'act_log.vehicle_number' ),
                Field::inst( 'act_log.container_number' ),
                Field::inst( 'act_log.vehicle_model' )
                    ->options( 'master_vehicle_type', 'id', 'vehicle_type' ),
                Field::inst( 'master_vehicle_type.vehicle_type' ),
                Field::inst( 'act_log.log_status' )
                    ->options( 'master_log_status', 'id', 'log_status' ),
                Field::inst( 'master_log_status.log_status' ),
                Field::inst( 'act_log.clearance_date' )
                    ->getFormatter( function ( $val, $data, $opts ) {
                        if ($val === "0000-00-00 00:00:00"){
                            echo "";
                        }else{
                            return date( 'H:i', strtotime( $val ) );
                        }
                    } ),
                Field::inst( 'act_log.outgoing_date' )
                    ->validator( 'Validate::dateFormat', array(
                        'empty' => false,
                        'format' => 'd M Y -- H:i'
                    ) )
                    ->getFormatter( function ( $val, $data, $opts ) {
                        if ($val === "0000-00-00 00:00:00"){
                            echo "";
                        }else{
                            return date( 'H:i', strtotime( $val ) );
                        }
                    } )
                    ->setFormatter( 'Format::datetime', array(
                        'from' => 'd M Y -- H:i',
                        'to' =>   'Y-m-d H:i:s'
                    ) )
                )
            ->leftJoin( 'master_log_category', 'master_log_category.id', '=', 'act_log.category' )
            ->leftJoin( 'master_expedition', 'master_expedition.expedition_code', '=', 'act_log.company_name' )
            ->leftJoin( 'master_vehicle_type', 'master_vehicle_type.id', '=', 'act_log.vehicle_model' )
            ->leftJoin( 'master_log_status', 'master_log_status.id', '=', 'act_log.log_status' )
            ->where('act_log.process_date','0000-00-00 00:00:00','<>')
            ->where('act_log.document_date','0000-00-00 00:00:00','<>')
            ->where('act_log.outgoing_date','0000-00-00 00:00:00')
            ->on( 'preEdit', function ( $editor, $values ) {            
                if ( $values['master_expedition']['expedition_type'] === "Internal" ){ 
                    $editor
                        ->field( 'act_log.log_status' )
                        ->setValue (5);
                    }
                else {
                    $editor
                        ->field( 'act_log.log_status' )
                        ->setValue (6);
                    }
                } )
            ->process($_POST)
            ->json();
    
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    User only edit a act_log.outgoing_date

    That's the issue then. You could submit the other data you require for your logic as a hidden field - hidden. Either that or you need to read it from the database.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    Thanks for the explanation Allan. I change my condition on client side using editor preOpen and works

    editor.on( 'preOpen', function ( e, mode, action ) { 
                    console.log (editor.val('master_expedition.expedition_type'));
                    if (editor.val('master_expedition.expedition_type') === 'Internal') {
                        editor.val( 'act_log.log_status',5 );
                    } else {
                        editor.val( 'act_log.log_status',6 );
                    }
                });
    
This discussion has been closed.