Debian Codegraph @

vendor/ipl/html/src/FormElement/PasswordElement.php

raw
1 <?php
2
3 namespace ipl\Html\FormElement;
4
5 use ipl\Html\Attributes;
6 use ipl\Html\Form;
7
8 class PasswordElement extends InputElement
9 {
10 /** @var string Dummy passwd of this element to be rendered */
11 public const DUMMYPASSWORD = '_ipl_form_5847ed1b5b8ca';
12
13 protected $type = 'password';
14
15 /** @var bool Status of the form */
16 protected $isFormValid = true;
17
18 /** @var bool Status indicating if the form got submitted */
19 protected $isFormSubmitted = false;
20
21 protected function registerAttributeCallbacks(Attributes $attributes)
22 {
23 parent::registerAttributeCallbacks($attributes);
24
25 $attributes->registerAttributeCallback(
26 'value',
27 function () {
28 if (
29 $this->hasValue()
30 && count($this->getValueCandidates()) === 1
31 && $this->isFormValid
32 && ! $this->isFormSubmitted
33 ) {
34 return self::DUMMYPASSWORD;
35 }
36
37 if (parent::getValue() === self::DUMMYPASSWORD && count($this->getValueCandidates()) > 1) {
38 return self::DUMMYPASSWORD;
39 }
40
41 return null;
42 }
43 );
44 }
45
46 public function onRegistered(Form $form)
47 {
48 $form->on(Form::ON_VALIDATE, function ($form) {
49 $this->isFormValid = $form->isValid();
50 });
51
52 $form->on(Form::ON_SENT, function ($form) {
53 $this->isFormSubmitted = $form->hasBeenSent();
54 });
55 }
56
57 public function getValue()
58 {
59 $value = parent::getValue();
60 $candidates = $this->getValueCandidates();
61 while ($value === self::DUMMYPASSWORD) {
62 $value = array_pop($candidates);
63 }
64
65 return $value;
66 }
67 }