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 Correctly mark ConfirmedPasswordField children as required #11099

Merged
Show file tree
Hide file tree
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
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);
}
}
79 changes: 79 additions & 0 deletions tests/php/Forms/ConfirmedPasswordFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,83 @@ public function testSetRequireExistingPasswordOnlyRunsOnce()
$field->setRequireExistingPassword(false);
$this->assertCount(2, $field->getChildren(), 'Current password field should not be removed');
}

public function provideRequired()
{
return [
'can be empty' => [true],
'cannot be empty' => [false],
];
}

/**
* @dataProvider provideRequired
*/
public function testRequired(bool $canBeEmpty)
{
$field = new ConfirmedPasswordField('Test');
$field->setCanBeEmpty($canBeEmpty);
$this->assertSame(!$canBeEmpty, $field->Required());
}

public function provideChildFieldsAreRequired()
{
return [
'not required' => [
'canBeEmpty' => true,
'required' => false,
'childrenRequired' => false,
'expectRequired' => false,
],
'required via validator' => [
'canBeEmpty' => true,
'required' => true,
'childrenRequired' => false,
'expectRequired' => true,
],
'children required directly' => [
'canBeEmpty' => true,
'required' => false,
'childrenRequired' => true,
'expectRequired' => true,
],
'required because cannot be empty' => [
'canBeEmpty' => false,
'required' => false,
'childrenRequired' => false,
'expectRequired' => true,
],
];
}

/**
* @dataProvider provideChildFieldsAreRequired
*/
public function testChildFieldsAreRequired(bool $canBeEmpty, bool $required, bool $childrenRequired, bool $expectRequired)
{
$form = new Form();
$field = new ConfirmedPasswordField('Test');
$field->setForm($form);
$field->setCanBeEmpty($canBeEmpty);
$requiredFields = [];
if ($required) {
$requiredFields[] = 'Test';
}
if ($childrenRequired) {
$requiredFields[] = 'Test[_Password]';
$requiredFields[] = 'Test[_ConfirmPassword]';
}
$form->setValidator(new RequiredFields($requiredFields));

$rendered = $field->Field();
$fieldOneRegex = '<input\s+type="password"\s+name="Test\[_Password\]"\s[^>]*?required="required"\s+aria-required="true"\s[^>]*\/>';
$fieldTwoRegex = '<input\s+type="password"\s+name="Test\[_ConfirmPassword\]"\s[^>]*?required="required"\s+aria-required="true"\s[^>]*\/>';
$regex = '/' . $fieldOneRegex . '.*' . $fieldTwoRegex . '/isu';

if ($expectRequired) {
$this->assertMatchesRegularExpression($regex, $rendered);
} else {
$this->assertDoesNotMatchRegularExpression($regex, $rendered);
}
}
}