Skip to content

Commit

Permalink
Update getter name to getCMSCompositeValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
cpenny committed May 28, 2020
1 parent 8ba6531 commit d4165db
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/en/02_Developer_Guides/03_Forms/01_Validation.md
Expand Up @@ -275,7 +275,7 @@ class MyController extends Controller

In the CMS, we're not creating the forms for editing CMS records. The `Form` instance is generated for us so we cannot
call `setValidator` easily. However, a `DataObject` can provide its' own `Validator` instance/s through the
`getCompositeValidator()` method. The CMS interfaces such as [LeftAndMain](api:SilverStripe\Admin\LeftAndMain),
`getCMSCompositeValidator()` method. The CMS interfaces such as [LeftAndMain](api:SilverStripe\Admin\LeftAndMain),
[ModelAdmin](api:SilverStripe\Admin\ModelAdmin) and [GridField](api:SilverStripe\Forms\GridField\GridField) will
respect the provided `Validator`/s and handle displaying error and success responses to the user.

Expand Down
13 changes: 7 additions & 6 deletions src/Forms/CompositeValidator.php
Expand Up @@ -12,20 +12,21 @@
* Once each Validator has generated its ValidationResult, the CompositeValidator will combine these results into a
* single ValidationResult. This single ValidationResult is what will be returned by the CompositeValidator.
*
* You can add Validators to the CompositeValidator in any DataObject by extending the getCompositeValidator() method:
* You can add Validators to the CompositeValidator in any DataObject by extending the getCMSCompositeValidator()
* method:
*
* public function getCompositeValidator(): CompositeValidator
* public function getCMSCompositeValidator(): CompositeValidator
* {
* $compositeValidator = parent::getCompositeValidator();
* $compositeValidator = parent::getCMSCompositeValidator();
*
* $compositeValidator->addValidator(RequiredFields::create(['MyRequiredField']));
*
* return $compositeValidator
* }
*
* Or by implementing the updateCompositeValidator() method in a DataExtension:
* Or by implementing the updateCMSCompositeValidator() method in a DataExtension:
*
* public function updateCompositeValidator(CompositeValidator $compositeValidator): void
* public function updateCMSCompositeValidator(CompositeValidator $compositeValidator): void
* {
* $compositeValidator->addValidator(RequiredFields::create(['AdditionalContent']));
* }
Expand Down Expand Up @@ -221,7 +222,7 @@ public function canBeCached(): bool
}

/**
* @internal This method may be updated to public in the future. Let us know if you feel there is a use case for it.
* @internal This method may be updated to public in the future. Let us know if you feel there's a use case for it
* @param int $key
* @return CompositeValidator
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Forms/DefaultFormFactory.php
Expand Up @@ -101,15 +101,15 @@ protected function getFormValidator(RequestHandler $controller = null, $name, $c
return null;
}

$compositeValidator = $context['Record']->getCompositeValidator();
$compositeValidator = $context['Record']->getCMSCompositeValidator();

// Extend validator - legacy support, will be removed in 5.0.0
foreach ($compositeValidator->getValidators() as $validator) {
$this->invokeWithExtensions('updateFormValidator', $validator, $controller, $name, $context);
}

// Extend validator - forward support, will be supported beyond 5.0.0
$this->invokeWithExtensions('updateCompositeValidator', $compositeValidator);
$this->invokeWithExtensions('updateCMSCompositeValidator', $compositeValidator);

return $compositeValidator;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/GridField/GridFieldDetailForm.php
Expand Up @@ -123,7 +123,7 @@ public function handleItem($gridField, $request)

// if no validator has been set on the GridField then use the Validators from the record.
if (!$this->getValidator()) {
$this->setValidator($record->getCompositeValidator());
$this->setValidator($record->getCMSCompositeValidator());
}

return $handler->handleRequest($request);
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/DataExtension.php
Expand Up @@ -141,7 +141,7 @@ public function updateCMSFields(FieldList $fields)
*
* @param CompositeValidator $compositeValidator
*/
public function updateCompositeValidator(CompositeValidator $compositeValidator): void
public function updateCMSCompositeValidator(CompositeValidator $compositeValidator): void
{
}

Expand Down
14 changes: 11 additions & 3 deletions src/ORM/DataObject.php
Expand Up @@ -2449,24 +2449,32 @@ public function getCMSActions()
}

/**
* When extending this class and overriding this method, you will need to instantiate the CompositeValidator by
* calling parent::getCMSCompositeValidator(). This will ensure that the appropriate extension point is also
* invoked.
*
* You can also update the CompositeValidator by creating an Extension and implementing the
* updateCMSCompositeValidator(CompositeValidator $compositeValidator) method.
*
* @see CompositeValidator for examples of implementation
* @return CompositeValidator
*/
public function getCompositeValidator(): CompositeValidator
public function getCMSCompositeValidator(): CompositeValidator
{
$compositeValidator = new CompositeValidator();

// Support for the old method during the deprecation period
if ($this->hasMethod('getCMSValidator')) {
Deprecation::notice(
'4.6',
'getCMSValidator() is removed in 5.0 in favour of getCompositeValidator()'
'getCMSValidator() is removed in 5.0 in favour of getCMSCompositeValidator()'
);

$compositeValidator->addValidator($this->getCMSValidator());
}

// Extend validator - forward support, will be supported beyond 5.0.0
$this->invokeWithExtensions('updateCompositeValidator', $compositeValidator);
$this->invokeWithExtensions('updateCMSCompositeValidator', $compositeValidator);

return $compositeValidator;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/php/Forms/CompositeValidatorTest.php
Expand Up @@ -191,8 +191,8 @@ public function testRemoveValidation(): void
$this->assertFalse($result->isValid());
$this->assertCount(1, $result->getMessages());

// Make sure it doesn't fail after removing validation AND has no errors (since calling validate should reset
// errors)
// Make sure it doesn't fail after removing validation AND has no errors (since calling validate should
// reset errors)
$compositeValidator->removeValidation();
$result = $form->validationResult();
$this->assertTrue($result->isValid());
Expand Down

0 comments on commit d4165db

Please sign in to comment.