Back to Top

How to create a password type field in admin configuration in magento2

Updated 23 February 2024

To create a field  as password type in which input text is abstruse at admin configuration

Use obscure as the field type and

Magento\Config\Model\Config\Backend\Encrypted as the backend_model

<field id='api_secret' translate='label' type='obscure' sortOrder='3' showInDefault='1' showInWebsite='1' showInStore='1'>
<label>Api key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>

after that you see that in configuration at admin panel a text field is created under name “Api key” and when you put or type some value in it then it will show as a password with dot sign.

Actually, obscure field type encrypts the value of fields.

Searching for an experienced
Magento 2 Company ?
Find out More

To get the decrypted value of  configuration’s obscure type field we will have to use

\Magento\Framework\Encryption\EncryptorInterface interface

<?php
Class Encryptor

protected $_enc;

public function __construct(
        \Magento\Framework\Encryption\EncryptorInterface $enc
    )
    {
        $this->_enc = $enc;
        parent::__construct($context);
    }
//here $val contain the encrypted configuration value 
$val = 'asdfgh-erp45dfrtgbnhjlkouojutywer';
$val = $this->_enc->decrypt($val);
?>

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home