Cancel Update Specific Field

Cancel Update Specific Field

modyking55modyking55 Posts: 14Questions: 7Answers: 0

$field->setFormatter(function ( $val, $data) use ($field) {
if($val != '*******')
{
$password = $val;
$expensiveHash = password_hash($password, PASSWORD_DEFAULT);
return $expensiveHash;
}
else {
** $field->set(false);**
return "";
}
});

How To Make That Work

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,983Questions: 87Answers: 421
    Answer ✓

    Hey modyking55,
    not sure what the question is but I figure you want to do something with passwords ... This works:

    The user can change the password and also needs to repeat the new password. We don't return the password from the server for safety reasons.

    Field::inst( 'user.password' )
        ->validator( function ( $val, $data, $opts ) {
            return validatorPassword($val);
        } )
        ->getFormatter( function ( $val, $data, $opts ) {
            return '';                   
        } )
        ->setFormatter( function($val, $data, $opts) {
            return password_hash($val, PASSWORD_DEFAULT, ['cost' => 14]);
        }),
    Field::inst( 'user.password AS repeatPassword' )->set( false )
        ->validator( function ( $val, $data, $opts ) {
            return validatorPassword($val, $data['user']);
        } )
        ->getFormatter( function ( $val, $data, $opts ) {
            return '';                   
        } ),
    

    If the user doesn't enter a new password we make sure it doesn't get updated. Take a look at the PHP server events please: https://editor.datatables.net/manual/php/events

    ->on('preCreate', function ( $editor, $values ) {
        if ($values['user']['password'] <= '') {
            $editor->field('user.password')->set( false );
        }
    })
    ->on('preEdit', function ( $editor, $id, $values ) {     
        if ($values['user']['password'] <= '') {
           $editor->field('user.password')->set( false );
        }
    })
    

    and my little validator ... whatever you need help yourself:

    function validatorPassword (&$val, &$data = null ) {    
        if ( ! is_null($data) ) {
            $repeatPassword = true;
            if ($val <= '' && $data['password'] <= '') {
                return true;
            }
        } else {
            $repeatPassword = false;
            if ($val <= '') {
                return true;
            }
        }
        
        if ($repeatPassword) {
            if ($data['password'] <= '') {
                if ($_SESSION['lang'] === 'de') {   
                    return 'Bitte zunächst oberhalb das neue Passwort eingeben!';
                } else {
                    return "Please enter new password above first!";
                }
            }
            if ($data['password'] !== $val) {
                if ($_SESSION['lang'] === 'de') {   
                    return 'Die beiden Passwörter sind nicht identisch!';
                } else {
                    return "The two passwords don't match";
                }
            }
        }    
        
        $uppercase = preg_match('@[A-Z]@', $val);
        $lowercase = preg_match('@[a-z]@', $val);
        $number    = preg_match('@[0-9]@', $val);
        
        if (!$uppercase || !$lowercase || !$number || strlen($val) < 8) {
            if ($_SESSION['lang'] === 'de') {   
                return 'Das Passwort muss mindestens eine Ziffer enthalten, 
                        mindestens einen Klein- und einen Großbuchstanben 
                        und muß mindestens 8 Zeichen lang sein!';
            } else {            
                return 'Password must contain at least one number, 
                        one lowercase and one upercase letter 
                        and be at least eight characters long!';
            }
        }
        return true;   
    }
    
    

    Maybe you would like to use Markdown in your posts .. very easy and all you really need is to use the triple back ticks as mentioned below ...

  • modyking55modyking55 Posts: 14Questions: 7Answers: 0

    Thanks that what i am looking for

This discussion has been closed.