PreCreate/preEdit problems

PreCreate/preEdit problems

th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

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

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    This is another error I get when I add a comma to the last parenthesis in the preCreate.

    <b>Fatal error</b>:  Call to undefined method DataTables\Editor\Field::on() in <b>/var/www/oesa_cms/users-con.php</b> on line <b>43</b><br />
    

    The syntax error I mentioned in my top post had to do with the comma not being after the parenthesis I believe.

  • allanallan Posts: 61,726Questions: 1Answers: 10,110 Site admin

    Looks like you are mixing Javascript and PHP there.

    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() !== '' ) )
    

    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 use strlen to get the string length, etc.

    Allan

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    I never even noticed. So I changed the code to the following...

    Field::inst( 'oesa_users.password' )
        ->get( false ) /* never read from the db*/
            ->on( 'preEdit', function ( $e, $id, $values ) {
                //--- Compare Password and Confirm fields. ---//
                if ( ( $values['oesa_users']['password'] === $values['oesa_users']['repassword'] ) && (strlen($values['oesa_users']['password']) >= 8) && ( $values['oesa_users']['password'] !== '' ) ) 
                {
                    return crypt( val, '$2a$SaltyHotDog' );
                //--- Check if both fields match. ---// 
                }   
                else if ( $values['oesa_users']['password'] !== $values['oesa_users']['repassword'] ) {
                    alert('Password and confirm password does not match!');
                    return false;
                    }
            } ),
    
            Field::inst( 'oesa_users.user_telephone' )...
    
    

    But I'm still getting the error

    <b>Fatal error</b>:  Call to undefined method DataTables\Editor\Field::on() in <b>/var/www/oesa_cms/users-con.php</b> on line <b>43</b><br />
    

    Line 43 is the ->on( 'preEdit'... line.

  • allanallan Posts: 61,726Questions: 1Answers: 10,110 Site admin
    Answer ✓

    Yup - there is no on method for the field. You need to attach event listeners to your Editor instance.

    Allan

This discussion has been closed.