Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix password replacement for unsubmitted and unsaved forms #142

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/FormElement/PasswordElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@ class PasswordElement extends InputElement
/** @var bool Status of the form */
protected $isFormValid = true;

/** @var bool Status indicating if the form got submitted */
protected $isFormSubmitted = false;

protected function registerAttributeCallbacks(Attributes $attributes)
{
parent::registerAttributeCallbacks($attributes);

$attributes->registerAttributeCallback(
'value',
function () {
if ($this->hasValue() && count($this->getValueCandidates()) === 1 && $this->isFormValid) {
if (
$this->hasValue()
&& count($this->getValueCandidates()) === 1
&& $this->isFormValid
&& ! $this->isFormSubmitted
) {
return self::DUMMYPASSWORD;
}

if (parent::getValue() === self::DUMMYPASSWORD) {
if (parent::getValue() === self::DUMMYPASSWORD && count($this->getValueCandidates()) > 1) {
return self::DUMMYPASSWORD;
}

Expand All @@ -40,6 +48,10 @@ public function onRegistered(Form $form)
$form->on(Form::ON_VALIDATE, function ($form) {
$this->isFormValid = $form->isValid();
});

$form->on(Form::ON_SENT, function ($form) {
$this->isFormSubmitted = $form->hasBeenSent();
});
}

public function getValue()
Expand Down
Loading