Automatic save setValue from SESSION to Field.

Automatic save setValue from SESSION to Field.

hyklhykl Posts: 48Questions: 19Answers: 5
edited March 2016 in Free community support

How are for save value from SESSION automatic after Create or Edit record to Field?

example:

Field::inst('username')
       ->setValue($_SESSION['name']),

It does not work :(

setFormatter or ??? - I does not know to set automatic save from SESSION.

This question has an accepted answers - jump to answer

Answers

  • btreebtree Posts: 99Questions: 14Answers: 11
    Answer ✓

    Hello,

    you need to define the action when it should save the value.

     ->on( 'preCreate', function ( $editor, $values ) {
            $editor
                ->field( 'username' )
                ->setValue( $_SESSION['name'] );
        } )
    
        ->on( 'preEdit', function ( $editor, $values ) {
            $editor
                ->field( 'username' )
                ->setValue( $_SESSION['name'] );
        } )
    

    Another solution would to use the setFormatter:

    Field::inst( 'username' )->setFormatter( function ( $val, $data, $opts ) {
        return $_SESSION['name'];
    } );
    

    Cheers
    Hannes

  • hyklhykl Posts: 48Questions: 19Answers: 5

    Hóóóóólááá, I am very happy :-) Thank you very much and thank you for fast time answer :-)

    Tested a it is OK :-)

This discussion has been closed.