Skip to content

Commit

Permalink
Display: add option to show/hide password in legacy FormValidator con…
Browse files Browse the repository at this point in the history
…trol
  • Loading branch information
AngelFQC committed Jan 27, 2025
1 parent 9225759 commit e3c621d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 32 additions & 1 deletion public/main/inc/lib/pear/HTML/QuickForm/password.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* @link http://pear.php.net/package/HTML_QuickForm
*/

use Chamilo\CoreBundle\Component\Utils\ActionIcon;

/**
* HTML class for a password type field
*
Expand All @@ -47,7 +49,7 @@ class HTML_QuickForm_password extends HTML_QuickForm_text
*/
public function __construct($elementName = null, $elementLabel = null, $attributes = null)
{
$attributes['class'] = isset($attributes['class']) ? $attributes['class'] : 'form-control';
$attributes['class'] = $attributes['class'] ?? '';
parent::__construct($elementName, $elementLabel, $attributes);
$this->setType('password');
}
Expand Down Expand Up @@ -95,4 +97,33 @@ public function getFrozenHtml()
return ('' != $value ? '**********' : ' ').
$this->_getPersistantData();
}

public function toHtml(): string
{
if (parent::isFrozen()) {
return parent::getFrozenHtml();
}

$attributes = $this->getAttributes();

$this->removeAttribute('show_hide');
$this->_attributes['class'] = ($attributes['class'] ?? '').' p-password-input ';

$input = parent::toHtml();

if (empty($attributes['show_hide'])) {
return $input;
}

$id = $attributes['id'] ?? '';

return '<div class="p-password p-component p-inputwrapper p-inputwrapper-filled p-icon-field p-icon-field-right">
'.$input.'
<span class="p-icon p-input-icon mdi mdi-'.ActionIcon::VISIBLE->value.'"></span>
</div>'
."<script>$('input#$id + .p-icon.mdi').click(() => {
var txtPasswd = $('input#$id')
txtPasswd.attr('type', txtPasswd.attr('type') === 'password' ? 'text' : 'password');
})</script>";
}
}
2 changes: 2 additions & 0 deletions public/main/inc/lib/pear/HTML/QuickForm/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public function toHtml()
return $this->getFrozenHtml();
}

$attributes = $this->getAttributes();

$this->_attributes['class'] = ($attributes['class'] ?? '').' p-inputtext p-component ';

if (FormValidator::LAYOUT_HORIZONTAL === $this->getLayout()) {
Expand Down

0 comments on commit e3c621d

Please # to comment.