PreCreate/preEdit problems
PreCreate/preEdit problems

So I'm trying to check a password field and if it is not blank, has 8 or more charactors, and matches the confirm password field then encrypt it and write it to the database. I would also like to use preEdit so users/admins can change the password but preEdit needs to check that the password and confirm password is the same and has changed. If I change any data other than the users password/confirm password a new password hash is written to the database (I'm assuming a blank or null was hashed).
So far I have this for the server side.
Field::inst( 'oesa_users.password' )
->get( false ) /* never read from the db*/
->on( 'preCreate', function ( $e, $data ) {
//--- Compare Password and Confirm fields. ---//
if ( ( this.field('oesa_users.password').val() === this.field('repassword').val() ) && (this.field('oesa_users.password').val().length >= 8) && ( this.field('oesa_users.password').val() !== '' ) )
{
return crypt( val, 'some_random_salt' );
//--- Check if both fields match. ---//
}
else if ( this.field('oesa_users.password').val() !== this.field('repassword').val() ) {
alert('Password and confirm password does not match!');
return false;
}
} ),
But I get a "syntax error, unexpected 'Field'...
And this for preEdit.
->on( 'preEdit', function ( $e, $id, $values ) {
if ( $values['password'] === '' ) {
$e->field( 'password' )->set( false );
}
} );
I just can't get either to work. I've been struggling with this for hours now.
This question has an accepted answers - jump to answer
Answers
This is another error I get when I add a comma to the last parenthesis in the preCreate.
The syntax error I mentioned in my top post had to do with the comma not being after the parenthesis I believe.
Looks like you are mixing Javascript and PHP there.
That's all Javascript syntax!
You'd actually use
$values['oesa_users']['password']
to get the data submitted from the client in the PHP code. Likewise usestrlen
to get the string length, etc.Allan
I never even noticed. So I changed the code to the following...
But I'm still getting the error
Line 43 is the ->on( 'preEdit'... line.
Yup - there is no
on
method for the field. You need to attach event listeners to yourEditor
instance.Allan