Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC RequiredFields allow_whitespace_only config #647

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()).
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved

```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
{
// ...

GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
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. See [form validation docs](/developer_guides/forms/validation/#requiredfields-and-whitespace) for more details.

## API changes

Expand Down
Loading