Skip to content

Commit

Permalink
FIX Correctly mark ConfirmedPasswordField children as required
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Dec 19, 2023
1 parent 6698e5a commit 2c03808
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/Forms/ConfirmedPasswordField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\Security\Authenticator;
use SilverStripe\Security\Security;
use SilverStripe\View\HTML;
Expand Down Expand Up @@ -200,7 +201,7 @@ public function Field($properties = [])
}
}

$fieldContent .= $field->FieldHolder();
$fieldContent .= $field->FieldHolder(['AttributesHTML' => $this->getAttributesHTMLForChild($field)]);
}

if (!$this->showOnClick) {
Expand Down Expand Up @@ -233,6 +234,19 @@ public function Field($properties = [])
);
}

public function Required()
{
return !$this->canBeEmpty || parent::Required();
}

public function setForm($form)
{
foreach ($this->getChildren() as $field) {
$field->setForm($form);
}
return parent::setForm($form);
}

/**
* Returns the children of this field for use in templating.
* @return FieldList
Expand Down Expand Up @@ -694,4 +708,18 @@ public function getRequireStrongPassword()
{
return $this->requireStrongPassword;
}

/**
* Get the AttributesHTML for a child field.
* Includes extra information the child isn't aware of on its own, such as whether
* it's required due to this field as a whole being required.
*/
private function getAttributesHTMLForChild(FormField $child): DBField
{
$attributes = $child->getAttributesHTML();
if (strpos($attributes, 'required="required"') === false && $this->Required()) {
$attributes .= 'required="required" aria-required="true"';
}
return DBField::create_field('HTMLFragment', $attributes);
}
}

0 comments on commit 2c03808

Please sign in to comment.