# Password updated empty when i edit with empty password field?

**URL:** <https://discourse.cakephp.org/t/password-updated-empty-when-i-edit-with-empty-password-field/2020>\
**Category:** Need Help\
**Created:** [February 9, 2017, 4:45am UTC](https://discourse.cakephp.org/t/password-updated-empty-when-i-edit-with-empty-password-field/2020 "2017-02-09T04:45:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SATHISH](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@SATHISH](https://discourse.cakephp.org/u/SATHISH)\
**Post date:** [February 9, 2017, 4:45am UTC](https://discourse.cakephp.org/t/password-updated-empty-when-i-edit-with-empty-password-field/2020/1 "2017-02-09T04:45:40Z")

</div>

Hi,  
I am using this code to validate in cakephp3.

```
> $validator
> ->requirePresence('password', 'create')
> ->add('password', 'match', ['rule' => ['compareWith', 'confirm_password'], 'message' => __('Password and Confirm Password should be same')])
> ->allowEmpty('password', 'update');

```

When i create new user, password field is validated. But i edit that user, i am not change password, it save empty in DB.  
I wont change password until password field is change.

How i fix it?

Thanks.

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [February 10, 2017, 2:11am UTC](https://discourse.cakephp.org/t/password-updated-empty-when-i-edit-with-empty-password-field/2020/2 "2017-02-10T02:11:52Z")

</div>

Because in the validation you allow the password to be empty.  
You should filter it in a setter, like this

```auto
// src/Model/Entity/User.php
protected function _setPassword($value)
{
    if ($value !== '') {
        $hasher = new DefaultPasswordHasher();

        return $hasher->hash($value);
    } else {
        return $this->password;
    }
}

```

---

<div class="post-metadata">

**Author:** ![SATHISH](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@SATHISH](https://discourse.cakephp.org/u/SATHISH)\
**Post date:** [February 10, 2017, 4:05am UTC](https://discourse.cakephp.org/t/password-updated-empty-when-i-edit-with-empty-password-field/2020/3 "2017-02-10T04:05:21Z")

</div>

thanks for you reply…👍
