I have a password field that when a new user is created I want to write the password to the database. That works fine and encrypts the password using the following.
$out=Editor::inst( $db, 'users' )
->field(
Field::inst( 'password' )
->get( false ) // never read from the db
->setFormatter ( function ( $val, $data ) {
return crypt( $val, '$somerandomsalthere$' );
}),
...
However if you edit an existing users information and do not specify a password a null is encrypted and the user's password is changed. I don't want to encrypt a blank/null password only a change such as a password reset.
I have been trying to figure this out and only have gotten this far unsuccessfully...
$out=Editor::inst( $db, 'users' )
->field(
Field::inst( 'password' )
->get( false ) // never read from the db
->validator( function ( $editor, $action, $data, $val ) {
if ( $action === Editor::ACTION_CREATE ) {
->setFormatter ( function ( $val, $data ) {
return crypt( $val, '$somerandomsalthere$' );
})
} else if ( $action === Editor::ACTION_EDIT ) {
Field( 'users' ).set(false);
return;
}
} ),
...
It doesn't like the ->setFormatter. Evidently it doesn't belong there. How else can I accomplish this?