1

I have changed the default minimum length of password from admin side but still I am unable to save customer from front end.The default length is being taken from some core constant apparently.

0

What is you Magento version?

Did you to clean the cache after your configuration change?

bin/magento cache:clean

|improve this answer|||||
  • yes I cleared cache after configuration save. I wanted to save number less than the 6, it saved on admin, but still when customer registered with giving password less than this constant const MIN_PASSWORD_LENGTH = 6;, it did not allow. My version is EE Magento ver. 2.1.5 – M Shoaib Qureshi Jul 26 '17 at 5:52
  • Strange, I tried with Magento EE 2.1.4 with a minimum password length at 5 and it works. – Mathieu Jul 26 '17 at 6:02
0

Got a solution for this. The file where the constant is declared is

CONSTANT that overrides the admin config:

const MIN_PASSWORD_LENGTH = 6;

File path:

/vendor/magento/module-customer/Model/Customer/Attribute/Backend/Password.php

Made my own file here:

/app/code/Vendor/Module/Model/Customer/Attribute/Backend/Password.php

here is my file code:

<?php

namespace Vendor\Module\Model\Customer\Attribute\Backend;

use Magento\Framework\Exception\LocalizedException;

/**
 * Customer password attribute backend
 */
class Password extends \Magento\Customer\Model\Customer\Attribute\Backend\Password
{
    /**
     * Min password length
     */
    const MIN_PASSWORD_LENGTH = 5;

    /**
     * Magento string lib
     *
     * @var \Magento\Framework\Stdlib\StringUtils
     */
    protected $string;

    /**
     * @param \Magento\Framework\Stdlib\StringUtils $string
     */
    public function __construct(\Magento\Framework\Stdlib\StringUtils $string)
    {
        $this->string = $string;
    }

    /**
     * Special processing before attribute save:
     * a) check some rules for password
     * b) transform temporary attribute 'password' into real attribute 'password_hash'
     *
     * @param \Magento\Framework\DataObject $object
     * @return void
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function beforeSave($object)
    {
        $password = $object->getPassword();

        $length = $this->string->strlen($password);
        if ($length > 0) {
            if ($length < self::MIN_PASSWORD_LENGTH) {
                throw new LocalizedException(
                    __('Please enter a password with at least %1 characters.', self::MIN_PASSWORD_LENGTH)
                );
            }

            if (trim($password) !== $password) {
                throw new LocalizedException(__('The password can not begin or end with a space.'));
            }

            $object->setPasswordHash($object->hashPassword($password));
        }
    }

    /**
     * @param \Magento\Framework\DataObject $object
     * @return bool
     */
    public function validate($object)
    {
        $password = $object->getPassword();
        if ($password && $password === $object->getPasswordConfirm()) {
            return true;
        }

        return parent::validate($object);
    }
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.