diff --git a/en/02_Developer_Guides/00_Model/09_Validation.md b/en/02_Developer_Guides/00_Model/09_Validation.md index 12730e04..9cb7c21d 100644 --- a/en/02_Developer_Guides/00_Model/09_Validation.md +++ b/en/02_Developer_Guides/00_Model/09_Validation.md @@ -58,7 +58,7 @@ class MyObject extends DataObject 'Postcode' => 'Varchar', ]; - public function validate() + public function validate(): ValidationResult { $result = parent::validate(); diff --git a/en/08_Changelogs/6.0.0.md b/en/08_Changelogs/6.0.0.md index 03f46aae..0a546173 100644 --- a/en/08_Changelogs/6.0.0.md +++ b/en/08_Changelogs/6.0.0.md @@ -31,6 +31,7 @@ title: 6.0.0 (unreleased) - [Bug fixes](#bug-fixes) - [API changes](#api-changes) - [Many renamed classes](#renamed-classes) + - [DataObject `validate()` signature change](#dataobject-validate) - [GraphQL removed from the CMS](#graphql-removed) - [`FormField` classes now use `FieldValidator` for validation](#formfield-validation) - [Most extension hook methods are now protected](#hooks-protected) @@ -928,6 +929,33 @@ The following classes have been changed when code was moved to the `silverstripe |`SilverStripe\UserForms\Form\UserFormsRequiredFields`|[`SilverStripe\UserForms\Form\UserFormsRequiredFieldsValidator`](api:SilverStripe\UserForms\Form\UserFormsRequiredFieldsValidator)| |`Symbiote\AdvancedWorkflow\Forms\AWRequiredFields`|[`Symbiote\AdvancedWorkflow\Forms\AWRequiredFieldsValidator`](api:Symbiote\AdvancedWorkflow\Forms\AWRequiredFieldsValidator)| +### DataObject `validate()` signature changed {#dataobject-validate} + +The method signature of the `validate()` method on `DataObject` has changed as it is now strongly typed. It now explicitly returns a [`ValidationResult`](api:SilverStripe\Core\Validation\ValidationResult). Previously the method still returned a `ValidationResult`, though this was not defined in the method signature. + +What this means is that you will now need to update the method sigature of any custom `validate()` methods you have in your `DataObject` subclasses to match the new signature. + +```php +// app/src/Models/MyDataObject.php +namespace App\Models; + +use SilverStripe\ORM\DataObject; +use SilverStripe\Core\Validation\ValidationResult; + +class MyDataObject extends DataObject +{ + // ... + public function validate(): ValidationResult + { + $result = parent::validate(); + if ($this->Title === 'Bad Title') { + $result->addError('Title cannot be "Bad Title"'); + } + return $result; + } +} +``` + ### GraphQL removed from the CMS {#graphql-removed} > [!NOTE]