Skip to content

Commit

Permalink
Merge branch '5' into 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Dec 9, 2024
2 parents 1b77aab + f30b9b2 commit c3fe58a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions en/02_Developer_Guides/03_Forms/01_Validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,43 @@ 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)

## API documentation

- [RequiredFields](api:SilverStripe\Forms\RequiredFields)
Expand Down

0 comments on commit c3fe58a

Please sign in to comment.