Editor, insert value to field NOT included in the table sent to client.

Editor, insert value to field NOT included in the table sent to client.

ztevieztevie Posts: 101Questions: 23Answers: 5
edited November 2016 in Free community support

Does anyone know: Is it possible to use the preCreate function below to insert the users.user_pwd value dynamically when a new user is created, without including Editor::inst user_pwd field and send it to client?
The below code works perfect, but I don't want to send the password to the client at all. I don't show it or edit it on the page, but it can be viewed anyway in the ajax arrays.
If I don't initiate this field preCreate will have the error missing field.
I understand preCreate expect values coming from the client, but can this be overridden somehow?

// DataTables PHP library
include( "../php/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\Upload,
DataTables\Editor\Validate;

/*
* Example PHP implementation used for the join.html example
*/
Editor::inst( $db, 'users', 'user_id' )
->field(
Field::inst( 'users.user_pwd' ),
Field::inst( 'users.user_fname' ),
Field::inst( 'users.user_lname' ),
Field::inst( 'users.workteam_id' )
->options( 'workteam', 'workteam_id', 'workteam_name' ),
Field::inst( 'workteam.workteam_name' ),
Field::inst( 'users.workdesc_id' )
->options( 'workdesc', 'workdesc_id', 'workdesc_name' ),
Field::inst( 'workdesc.workdesc_name' )
)
->leftJoin( 'workteam', 'workteam.workteam_id', '=', 'users.workteam_id' )
->leftJoin( 'workdesc', 'workdesc.workdesc_id', '=', 'users.workdesc_id' )
->join(
Mjoin::inst( 'roles' )
->link( 'users.user_id', 'permissions.user_id' )
->link( 'roles.role_id', 'permissions.role_id' )
->order( 'role_desc asc' )
->fields(
Field::inst( 'role_id' )
->validator( 'Validate::required' )
->options( 'roles', 'role_id', 'role_desc' ),
Field::inst( 'role_desc' )
)
)
->on( 'preCreate', function ( $editor, $values ) {
$editor
->field( 'users.user_pwd' )
->setValue(password_hash('qwe234', PASSWORD_DEFAULT));
} )
->process($_POST)
->json();

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • ztevieztevie Posts: 101Questions: 23Answers: 5

    OK, like so often before when coding. As soon as I'm giving up searching for an answer and post a question, I manage to find a way...
    Please tell me if this is a acceptable way of doing things?
    Now the user_pwd sent to client is nothing "", still, when creating a user a dynamically created password is created in the database, And when updating a user the original password is not changed...
    I just added a getFormatter function in the Field::inst, like so:

    Field::inst( 'users.user_pwd' )
    ->set( Field::SET_CREATE )
    ->getFormatter( function ( $val ) {
    return "";
    } ),

  • ztevieztevie Posts: 101Questions: 23Answers: 5

    Even easier is the getValue method....

    Field::inst( 'users.user_pwd' )
    ->set( Field::SET_CREATE )
    ->getValue(""),

  • allanallan Posts: 61,950Questions: 1Answers: 10,158 Site admin
    Answer ✓

    Thanks for posting back. You are absolutely correct. You can also use ->get( false ) to stop the field being read from the database at all.

    Allan

  • ztevieztevie Posts: 101Questions: 23Answers: 5
    edited November 2016

    Aha, even better. Will try that.
    Thanks!

This discussion has been closed.