Skip to content

Commit

Permalink
DOC RequiredFields allow_whitespace_only config
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Dec 5, 2024
1 parent abf9f5f commit 03d8f95
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions en/02_Developer_Guides/03_Forms/01_Validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,39 @@ class MyPage extends Page
> You can also update the `CompositeValidator` by creating an `Extension` and implementing the
> `updateCMSCompositeValidator()` method.
### `RequiredFields` and whitespace

By default, `RequiredFields` will consider a field with only whitespace as a valid value. You an change this behavior with the [`allow_whitespace_only`](api:SilverStripe\Forms\RequiredFields->allow_whitespace_only) global configuration, or on a per-instance basis using [`setAllowWhitespaceOnly()`](api:SilverStripe\Forms\RequiredFields::setAllowWhitespaceOnly()).

```yml
# global configuration
SilverStripe\Forms\RequiredFields:
allow_whitespace_only: false
```
```php
namespace App\PageType;

use Page;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\RequiredFields;

class MyPage extends Page
{
// ...

public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$requiredFields = RequiredFields::create(['MyRequiredField']);
// per instance configuration, will override global configuration
$requiredFields->setAllowWhitespaceOnly(false);
$validator->addValidator($requiredFields);
return $validator;
}
}
```

## Related lessons

- [Introduction to frontend forms](https://www.silverstripe.org/learn/lessons/v4/introduction-to-frontend-forms-1)
Expand Down
1 change: 1 addition & 0 deletions en/08_Changelogs/5.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ The `SilverStripe\CMS\Model\SiteTree.DESCRIPTION` localisation key (along with t
- A new [`DBDatetime::getTimeBetween()`](api:SilverStripe\ORM\FieldType\DBDatetime::getTimeBetween()) method has been added. This method returns the amount of time that has passed between two `DBDateTime` objects as a human-readable string.
- A new [`AbstractQueuedJob::getQueue()`](api:Symbiote\QueuedJobs\Services\AbstractQueuedJob::getQueue()) static method has been added to get the correct queue constant from a given string or int.
- New [`GridFieldFilterHeader::setPlaceHolderText()`](api:SilverStripe\Forms\GridField::GridFieldFilterHeader::setPlaceHolderText()) and [`GridFieldFilterHeader::getPlaceHolderText()`](api:SilverStripe\Forms\GridField::GridFieldFilterHeader::getPlaceHolderText()) methods have been added which provide a way to override the `GridFieldFilterHeader` search field placeholder text if the dynamically generated text doesn't suit your use case.
- [`RequiredFields`](api:SilverStripe\Forms\RequiredFields) can be to set to determine whether a whitespace only value, such as a single space character, is considered a valid value or not. This can be set globally via the [`RequiredFields.allow_whitespace_only`](api:SilverStripe\Forms\RequiredFields->allow_whitespace_only) config, which has a default value of `true` to retain backwards compatibility. This can also be set on a per-instance basis via [`RequiredFields::setAllowWhitespaceOnly()`](api:SilverStripe\Forms\RequiredFields::setAllowWhitespaceOnly()) which will override the global config.

## API changes

Expand Down

0 comments on commit 03d8f95

Please sign in to comment.