You could validate the length in the in the validation()
function, then hash the password in the `afterValidation() function.
Just make sure that you check for changes using $this->keepSnapshots(true);
in your initialize()
function so that you only validate the password length when it was actually changed. You can check for changes like so:
public function initialize() {
// Keep a snapshot of the model
$this->keepSnapshots(true);
}
public function validation(){
// Setup the validator
$this->validate(new \Phalcon\Mvc\Model\Validator\StringLength(array(
'field' => 'password',
'max' => 60,
'min' => 6,
'messageMaximum' => 'Your password is too long.',
'messageMinimum' => 'Your password is too short.'
)));
return !$this->validationHasFailed();
}
public function afterValidation(){
// If there has been a change in the password, re-hash it
if($this->hasChanged('password')){
$this->password = password_hash($this->password, PASSWORD_BCRYPT)
}
}
More Info on Recording Snapshots
I didn't test that code specifically but it should point you in the right direction