diff --git a/src/Forms/ConfirmedPasswordField.php b/src/Forms/ConfirmedPasswordField.php index 479c89d597a..ce3eccaebe5 100644 --- a/src/Forms/ConfirmedPasswordField.php +++ b/src/Forms/ConfirmedPasswordField.php @@ -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; @@ -200,7 +201,7 @@ public function Field($properties = []) } } - $fieldContent .= $field->FieldHolder(); + $fieldContent .= $field->FieldHolder(['AttributesHTML' => $this->getAttributesHTMLForChild($field)]); } if (!$this->showOnClick) { @@ -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 @@ -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); + } }