Dealing with password field in editor, please help.

Dealing with password field in editor, please help.

edanyildizedanyildiz 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:

Answers

  • allanallan Posts: 63,834Questions: 1Answers: 10,518 Site admin
    Answer ✓

    Hi,

    This sounds like a perfect use case for server-side events. Use the preCreate event to add another validator to the users.user_pass field which requires the user to submit a value.

    Allan

  • borrislborrisl Posts: 7Questions: 3Answers: 1
    edited December 2016 Answer ✓

    This is how I resolved it:

     editor.on( 'preSubmit', function ( e, o, action ) {
        if ( action == 'create' ) {
            // stuff
            var password = editor.field('password');
            var password2 = editor.field('password2');
    
            if(!password.isMultiValue()) {
                if (!password.val()) {
                    password.error('Password must be provided for administrative privileges');
                }
    
                if (password.val().length >= 40) {
                    password.error('The password length must be less than 40 characters');
                }
    
                if (password.val() != password2.val()) {
                    password.error('Passwords do no match')
                 }
             }
         }
    
         if ( this.inError() ) {
             return false;
          }
      }
    } );
    

    You can validate it before it is passed to the server.

  • edanyildizedanyildiz Posts: 43Questions: 13Answers: 4

    Thank you All.
    Both of the solutions are good, i think i am going to use Allan's server side solution.

  • edanyildizedanyildiz Posts: 43Questions: 13Answers: 4
    Answer ✓

    working like a charm :9
    Here is the working code.
    Thanks.

           Field::inst( 'users.user_pass' )
       ->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 );
        }
        } )
    
    
        //if create - validate for pass
        ->on( 'preCreate', function ( $editor, $values ) {
        $editor
        ->field( 'users.user_pass' )
        ->validator( 'Validate::notEmpty' );
        } )
    
        ->process( $_POST )
    
        ->json();
    
This discussion has been closed.