Skip to content

Commit

Permalink
Merge pull request #677 from creative-commoners/pulls/6.0/validation-…
Browse files Browse the repository at this point in the history
…values

DOC Show more information in ValidationException message
  • Loading branch information
GuySartorelli authored Feb 10, 2025
2 parents 772b285 + 955ea81 commit 1ac73e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions en/02_Developer_Guides/00_Model/09_Validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ sub-classes to specify custom validation, or use the `updateValidate()` extensio

Invalid objects won't be able to be written - a [`ValidationException`](api:SilverStripe\Core\Validation\ValidationException) will be thrown and no write will occur.

If appropriate, you can call [`ValidationResult::setModelClass()`](api:SilverStripe\Core\Validation\ValidationResult::setModelClass())
and [`ValidationResult::setRecordID()`](api:SilverStripe\Core\Validation\ValidationResult::setRecordID()) on your `ValidationResult` instance to set the class and ID of the object being validated. This additional info will show in the validation error messages in a CLI context, as well as in a non-CLI context if the current controller is an instance or a subclass of a controller configured in the [`ValidationException.show_additional_info_non_cli_controllers`](api:SilverStripe\Core\Validation\ValidationException->show_additional_info_non_cli_controllers) configuration.

The return value of `validate()` is a [`ValidationResult`](api:SilverStripe\Core\Validation\ValidationResult) object.

```php
Expand Down
9 changes: 9 additions & 0 deletions en/08_Changelogs/6.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ title: 6.0.0 (unreleased)
- [Update JS MIME type, remove `type` in `<script>` tags](#js-mime-type-update)
- [`getSchemaDataDefaults()` now includes attributes](#formfield-schema-data)
- [Remember me token rotation](#remember-me-token-rotation)
- [Validation exceptions shows additional info in some contexts](#validation-exception-info)
- [Full list of removed and changed API (by module, alphabetically)](#api-removed-and-changed)

## Change to commercially supported modules {#changes-to-support}
Expand Down Expand Up @@ -1176,6 +1177,14 @@ If you were calling `getSchemaData()` in your `getAttributes()` method in a `For

The related `onAfterRenewToken` extension hook has been renamed to `onAfterRenewSession`, and is triggered at the same logical step in the session renewal process.

### Validation exceptions shows additional info in some contexts {#validation-exception-info}

[`ValidationResult`](api:SilverStripe\Core\Validation\ValidationResult) objects created by [`DataObject::validate()`](api:SilverStripe\ORM\DataObject::validate()) now includes additional information about validation exceptions including the field name that failed validation, and if applicable the model class and record ID. This additional information will be included in the validation message by [`ValidationException`](api:SilverStripe\Core\Validation\ValidationException) if the validation exception happened in a CLI context, or in an HTTP context if the current controller is an instance contained in the [`ValidationException.show_additional_info_non_cli_controllers`](api:SilverStripe\Core\Validation\ValidationException->show_additional_info_non_cli_controllers) configuration or a subclass. By default only [`DevelopmentAdmin`](api:SilverStripe\Dev\DevelopmentAdmin) and subclasses are configured to show additonal information.

This is done to help developers debug validation issues that happen outside of a UX context more easily, such as validation exceptions that occur during a `dev/build`.

The value of the field that failed validation is intentially not included in the output, as it could contain sensitive information e.g. an API token.

### Full list of removed and changed API (by module, alphabetically) {#api-removed-and-changed}

<!--- Changes below this line will be automatically regenerated -->
Expand Down

0 comments on commit 1ac73e3

Please sign in to comment.