Skip to content

Commit

Permalink
DOC Document FieldsValidator (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Aug 14, 2023
1 parent bc35d68 commit ef31438
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
7 changes: 5 additions & 2 deletions en/02_Developer_Guides/03_Forms/00_Introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ $form = new Form(
$name, // name of the method that returns this form on the controller
FieldList $fields, // list of FormField instances
FieldList $actions, // list of FormAction instances
$required // optional use of RequiredFields object
$validator // optional use of Validator object
);
```

Expand Down Expand Up @@ -343,7 +343,10 @@ validating its' own data value.
For more information, see the [Form Validation](validation) documentation.

```php
$validator = new SilverStripe\Forms\RequiredFields([
use SilverStripe\Forms\Form;
use SilverStripe\Forms\RequiredFields;

$validator = new RequiredFields([
'Name',
'Email'
]);
Expand Down
20 changes: 17 additions & 3 deletions en/02_Developer_Guides/03_Forms/01_Validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ icon: check-square

# Form Validation

Silverstripe CMS provides server-side form validation out of the box through the [Validator](api:SilverStripe\Forms\Validator) class and its' child class
[RequiredFields](api:SilverStripe\Forms\RequiredFields). A single `Validator` instance is set on each `Form`. Validators are implemented as an argument to
Silverstripe CMS provides server-side form validation out of the box through the [Validator](api:SilverStripe\Forms\Validator) class and its' child classes
(see [available validators](#available-validators) below). A single `Validator` instance is set on each `Form`. Validators are implemented as an argument to
the [Form](api:SilverStripe\Forms\Form) constructor or through the function `setValidator`.

```php
Expand Down Expand Up @@ -181,6 +181,20 @@ class Page_Controller extends ContentController
}
```

## Available validators

The Silverstripe framework comes with the following built-in validators:

- [`CompositeValidator`](api:SilverStripe\Forms\CompositeValidator)
A container for additional validators. You can implement discrete validation logic in multiple `Validator` subclasses and apply them _all_ to a
given form by putting them inside a `CompositeValidator`. The `CompositeValidator` doesn't have perform any validation by itself.
- [`FieldsValidator`](api:SilverStripe\Forms\FieldsValidator)
Simply calls [`validate()`](api:SilverStripe\Forms\FormField::validate()) on all data fields in the form, to ensure fields have valid values.
- [`RequiredFields`](api:SilverStripe\Forms\RequiredFields)
Validates that fields you declare as "required" have a value.

There are additional validators available in community modules, and you can implement your own validators by subclassing the abstract `Validator` class.

## Exempt validation actions

In some cases you might need to disable validation for specific actions. E.g. actions which discard submitted
Expand Down Expand Up @@ -274,7 +288,7 @@ class MyController extends Controller
### Validation in the CMS

In the CMS, we're not creating the forms for editing CMS records. The `Form` instance is generated for us so we cannot
call `setValidator` easily. However, a `DataObject` can provide its' own `Validator` instance/s through the
call `setValidator` easily. However, a `DataObject` can provide its own `Validator` instance/s through the
`getCMSCompositeValidator()` method. The CMS interfaces such as [LeftAndMain](api:SilverStripe\Admin\LeftAndMain),
[ModelAdmin](api:SilverStripe\Admin\ModelAdmin) and [GridField](api:SilverStripe\Forms\GridField\GridField) will
respect the provided `Validator`/s and handle displaying error and success responses to the user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ This data is used to create an email, which you then send to the address you cho
The final thing we do is return a 'thank you for your feedback' message to the user. To do this we override some of the methods called in the template by returning an array. We return the HTML content we want rendered instead of the usual CMS-entered content, and we return false for Form, as we don't want the form to render.


##How to add form validation
## How to add form validation

All forms have some basic validation built in – email fields will only let the user enter email addresses, number fields will only accept numbers, and so on. Sometimes you need more complicated validation, so you can define your own validation by extending the Validator class.

Expand Down
4 changes: 1 addition & 3 deletions en/02_Developer_Guides/11_Integration/00_CSV_Import.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ use SilverStripe\Forms\Form;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FileField;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Dev\CsvBulkLoader;
use SilverStripe\Control\Controller;

Expand All @@ -114,8 +113,7 @@ class MyController extends Controller
),
new FieldList(
new FormAction('doUpload', 'Upload')
),
new RequiredFields()
)
);
return $form;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use SilverStripe\Forms\Form;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FileField;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\FieldsValidator;
use SilverStripe\Dev\CsvBulkLoader;
use SilverStripe\Control\Controller;

Expand Down Expand Up @@ -45,7 +45,7 @@ class MyController extends Controller
new FieldList(
new FormAction('doUpload', 'Upload')
),
new RequiredFields()
new FieldsValidator()
);
return $form;
}
Expand Down

0 comments on commit ef31438

Please sign in to comment.