Dealing with password field in editor, please help.
Dealing with password field in editor, please help.
edanyildiz
Posts: 43Questions: 13Answers: 4
Dear All,
I have a scenerio like below,
It is already working, but there is a problem;
- When the user details are edited, it's working like a charm. Since the pass field is empty editor does not update the pass.
- But when i create a new user, i need to validate if the pass if empty.
How can i enable validation only in create mode?
Field::inst( 'users.user_pass' )
//->validator( 'Validate::notEmpty' )
->setFormatter( function ( $val ) { return password_hash($val, PASSWORD_DEFAULT); } )
->getFormatter( function ( $val, $data, $opts ) { return null;})
)
//if edit mode and password is blank, do not save.
->on( 'preEdit', function ( $e, $id, $values ) {
if ( $values['users']['user_pass'] === '' ) {
$e->field( 'users.user_pass' )->set( false );
}
} )
->process( $_POST )
->json();
This question has accepted answers - jump to:
This discussion has been closed.
Answers
Hi,
This sounds like a perfect use case for server-side events. Use the
preCreate
event to add another validator to theusers.user_pass
field which requires the user to submit a value.Allan
This is how I resolved it:
You can validate it before it is passed to the server.
Thank you All.
Both of the solutions are good, i think i am going to use Allan's server side solution.
working like a charm :9
Here is the working code.
Thanks.