I want to pass some extra fields with my controller php file

I want to pass some extra fields with my controller php file

lavanialavania Posts: 12Questions: 5Answers: 0
edited December 2023 in Editor

I have a mysql table, all the fields of table are not shown in editor datatable, on update of some field. I want to pass extra fields data from my controller the Data is Like who created the record, or who is updating the data or some other fields.

Some one please help me. I am a new user.

<?php

/*
* Example PHP implementation used for the index.html example
*/

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

// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
DataTables\Editor\ValidateOptions;
...
$ok = "John Doe"; // I want to pass this value to my sa_added_by_id field
...
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'social_activity ' )
->fields(
Field::inst( 'sa_title' )
->validator( Validate::notEmpty( ValidateOptions::inst()
->message( 'A title is required' )
) ),
Field::inst( 'sa_place' )
->validator( Validate::notEmpty( ValidateOptions::inst()
->message( 'A place name is required' )
) ),
Field::inst( 'sa_activity_type' ),

    Field::inst( 'sa_lat' )
        ->validator( Validate::numeric() )
        ->setFormatter( Format::ifEmpty(null) ),
    Field::inst( 'sa_lng' )
        ->validator( Validate::numeric() )
        ->setFormatter( Format::ifEmpty(null) ),
    Field::inst( 'sa_date' )
        ->validator( Validate::dateFormat( 'Y-m-d' ) )
        ->getFormatter( Format::dateSqlToFormat( 'Y-m-d' ) )
        ->setFormatter( Format::dateFormatToSql('Y-m-d' ) )
)
->debug(true)
->process( $_POST )
->json();

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    On the client-side you can use preSubmit to modify the data being submitted to the server-side. However, it would be best to use the user session information you'll likely have at the server-side (I assume?) so as to ensure the data is accurate. The server-side events can be used for that. Specifically, have a look at this section.

    Allan

  • lavanialavania Posts: 12Questions: 5Answers: 0

    Thanks for your response it works partially for me, see this screen

    Debug Screen

    My code

    <?php

    /*
    * Example PHP implementation used for the index.html example
    */

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

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

    $ok = "John Doe 2";
    ...

    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'social_activity ' )
    ->fields(
    Field::inst( 'sa_title' )
    ->validator( Validate::notEmpty( ValidateOptions::inst()
    ->message( 'A title is required' )
    ) ),
    Field::inst( 'sa_place' )
    ->validator( Validate::notEmpty( ValidateOptions::inst()
    ->message( 'A place name is required' )
    ) ),
    Field::inst( 'sa_activity_type' ),

        Field::inst( 'sa_lat' )
            ->validator( Validate::numeric() )
            ->setFormatter( Format::ifEmpty(null) ),
        Field::inst( 'sa_lng' )
            ->validator( Validate::numeric() )
            ->setFormatter( Format::ifEmpty(null) ),
        Field::inst( 'sa_date' )
            ->validator( Validate::dateFormat( 'Y-m-d' ) )
            ->getFormatter( Format::dateSqlToFormat( 'Y-m-d' ) )
            ->setFormatter( Format::dateFormatToSql('Y-m-d' ) ),
        Field::inst( 'sa_added_by_id' )->set( Field::SET_CREATE ),
        Field::inst( 'sa_added_by_id' )->set( Field::SET_EDIT )
    )
     ->on( 'preCreate', function ( $editor, &$values ) {
        $editor
            ->field( 'sa_added_by_id' )
            ->setValue( 'John Doe' );
     } )
    ->on( 'preEdit', function ( $editor, $id, &$values ) {
        $editor
            ->field( 'sa_added_by_id' )
            ->setValue( $ok );
     } )
    ->process( $_POST )
    ->json();
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Yes, with PHP you need to tell it that you want to make a variable from a higher scope available in an anonymous function:

    function ( $editor, $id, &$values ) use ( $ok ) {
    

    Move details available in the PHP manual (example 3 on that page).

    Allan

Sign In or Register to comment.