Merge pull request #11395 from silverstripe-terraformers/bugfix/member-validator

FIX: Member form validator fix.
This commit is contained in:
Guy Sartorelli 2024-09-27 14:51:23 +12:00 committed by GitHub
commit 8a538f78ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 0 deletions

View File

@ -15,6 +15,7 @@ use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\TestMailer;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\ConfirmedPasswordField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
@ -696,6 +697,20 @@ class Member extends DataObject
return $validator;
}
public function getCMSCompositeValidator(): CompositeValidator
{
// Add the member validator before extension point, so it's much easier to customise this
// via an extension
$this->beforeExtending(
'updateCMSCompositeValidator',
function (CompositeValidator $compositeValidator): void {
$memberValidator = $this->getValidator();
$compositeValidator->addValidator($memberValidator);
}
);
return parent::getCMSCompositeValidator();
}
/**
* Temporarily act as the specified user, limited to a $callback, but

View File

@ -0,0 +1,21 @@
<?php
namespace SilverStripe\Core\Tests\Validation;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Security\Member;
use SilverStripe\Security\Member_Validator;
class MemberValidatorTest extends SapphireTest
{
protected $usesDatabase = false;
public function testMemberValidator(): void
{
$member = new Member();
$compositeValidator = $member->getCMSCompositeValidator();
$memberValidators = $compositeValidator->getValidatorsByType(Member_Validator::class);
$this->assertCount(1, $memberValidators, 'We expect exactly one member validator');
}
}