Skip to content

Commit

Permalink
API Rename validator classes
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Dec 2, 2024
1 parent 47cff0e commit fb02f4a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
8 changes: 4 additions & 4 deletions en/02_Developer_Guides/03_Forms/00_Introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use PageController;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;
use SilverStripe\Forms\TextField;

class MyFormPageController extends PageController
Expand All @@ -63,7 +63,7 @@ class MyFormPageController extends PageController
FormAction::create('doSayHello')->setTitle('Say hello')
);

$required = RequiredFields::create('Name');
$required = RequiredFieldsValidator::create('Name');

$form = Form::create($this, 'HelloForm', $fields, $actions, $required);

Expand Down Expand Up @@ -414,7 +414,7 @@ namespace App\PageType;

use PageController;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;

class MyFormPageController extends PageController
{
Expand All @@ -424,7 +424,7 @@ class MyFormPageController extends PageController
{
// ...

$validator = RequiredFields::create([
$validator = RequiredFieldsValidator::create([
'Name',
'Email',
]);
Expand Down
16 changes: 8 additions & 8 deletions en/02_Developer_Guides/03_Forms/01_Validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use PageController;
use SilverStripe\Forms\EmailField;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;
use SilverStripe\Forms\TextField;

class MyFormPageController extends PageController
Expand All @@ -46,7 +46,7 @@ class MyFormPageController extends PageController
);

// the fields 'Name' and 'Email' are required.
$required = RequiredFields::create([
$required = RequiredFieldsValidator::create([
'Name', 'Email',
]);

Expand Down Expand Up @@ -237,7 +237,7 @@ class MyFormPageController extends PageController

public function doSubmitForm($data, $form)
{
// At this point, RequiredFields->isValid() will have been called already,
// At this point, RequiredFieldsValidator->isValid() will have been called already,
// so we can assume that the values exist. Say we want to make sure that email hasn't already been used.

$check = Member::get()->filter('Email', $data['Email'])->first();
Expand Down Expand Up @@ -265,9 +265,9 @@ 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)
- [`FormFieldsValidator`](api:SilverStripe\Forms\FormFieldsValidator)
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)
- [`RequiredFieldsValidator`](api:SilverStripe\Forms\RequiredFieldsValidator)
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.
Expand Down Expand Up @@ -385,7 +385,7 @@ namespace App\PageType;
use Page;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;
use SilverStripe\Forms\TextField;

class MyPage extends Page
Expand All @@ -408,7 +408,7 @@ class MyPage extends Page
public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create([
$validator->addValidator(RequiredFieldsValidator::create([
'MyRequiredField',
]));
return $validator;
Expand All @@ -422,5 +422,5 @@ class MyPage extends Page
## API documentation

- [RequiredFields](api:SilverStripe\Forms\RequiredFields)
- [RequiredFieldsValidator](api:SilverStripe\Forms\RequiredFieldsValidator)
- [Validator](api:SilverStripe\Forms\Validator)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HeaderField;
use SilverStripe\Forms\NumericField;
use SilverStripe\Forms\OptionsetField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;

class SearchPageController extends PageController
{
Expand Down Expand Up @@ -57,7 +57,7 @@ class SearchPageController extends PageController
FormAction::create('doSearchForm', 'Search')
);

$required = RequiredFields::create([
$required = RequiredFieldsValidator::create([
'Type',
]);

Expand Down Expand Up @@ -92,7 +92,7 @@ use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HeaderField;
use SilverStripe\Forms\NumericField;
use SilverStripe\Forms\OptionsetField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;

class SearchForm extends Form
{
Expand Down Expand Up @@ -127,7 +127,7 @@ class SearchForm extends Form
FormAction::create('doSearchForm', 'Search')
);

$required = RequiredFields::create([
$required = RequiredFieldsValidator::create([
'Type',
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ The final thing we do is return a 'thank you for your feedback' message to the u

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.

The framework comes with a predefined validator called [RequiredFields](api:SilverStripe\Forms\RequiredFields), which performs the common task of making sure particular fields are filled out. Below is the code to add validation to a contact form:
The framework comes with a predefined validator called [`RequiredFieldsValidator`](api:SilverStripe\Forms\RequiredFieldsValidator), which performs the common task of making sure particular fields are filled out. Below is the code to add validation to a contact form:

```php
namespace App\PageType;

use PageController;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;

class ContactPageController extends PageController
{
Expand All @@ -167,10 +167,10 @@ class ContactPageController extends PageController
public function getForm()
{
// ...
$validator = RequiredFields::create('Name', 'Message');
$validator = RequiredFieldsValidator::create('Name', 'Message');
return Form::create($this, 'Form', $fields, $actions, $validator);
}
}
```

We've created a RequiredFields object, passing the name of the fields we want to be required. The validator we have created is then passed as the fifth argument of the form constructor. If we now try to submit the form without filling out the required fields, JavaScript validation will kick in, and the user will be presented with a message about the missing fields. If the user has JavaScript disabled, PHP validation will kick in when the form is submitted, and the user will be redirected back to the Form with messages about their missing fields.
We've created a RequiredFieldsValidator object, passing the name of the fields we want to be required. The validator we have created is then passed as the fifth argument of the form constructor. If we now try to submit the form without filling out the required fields, JavaScript validation will kick in, and the user will be presented with a message about the missing fields. If the user has JavaScript disabled, PHP validation will kick in when the form is submitted, and the user will be redirected back to the Form with messages about their missing fields.
12 changes: 6 additions & 6 deletions en/02_Developer_Guides/03_Forms/How_Tos/06_Handle_Nested_data.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;
use SilverStripe\Forms\TextField;

class MyController extends Controller
Expand Down Expand Up @@ -99,7 +99,7 @@ class MyController extends Controller
FieldList::create([
FormAction::create('doSubmitForm', 'Submit'),
]),
RequiredFields::create([
RequiredFieldsValidator::create([
'Name',
'Teams',
'ID',
Expand Down Expand Up @@ -149,7 +149,7 @@ use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;
use SilverStripe\Forms\TextField;

class MyController extends Controller
Expand Down Expand Up @@ -177,7 +177,7 @@ class MyController extends Controller
FieldList::create([
FormAction::create('doSubmitForm', 'Submit'),
]),
RequiredFields::create([
RequiredFieldsValidator::create([
'Name',
'HometownTeam.Name',
'ID',
Expand Down Expand Up @@ -232,7 +232,7 @@ use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;
use SilverStripe\Forms\TextField;

class MyController extends Controller
Expand Down Expand Up @@ -262,7 +262,7 @@ class MyController extends Controller
FieldList::create([
FormAction::create('doSubmitForm', 'Submit'),
]),
RequiredFields::create([
RequiredFieldsValidator::create([
'Name',
'MyTeams[]',
'ID',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use App\Model\MyDataObject;
use SilverStripe\Control\Controller;
use SilverStripe\Dev\CsvBulkLoader;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FieldsValidator;
use SilverStripe\Forms\FormFieldsValidator;
use SilverStripe\Forms\FileField;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
Expand Down Expand Up @@ -47,7 +47,7 @@ class MyController extends Controller
FieldList::create(
FormAction::create('doUpload', 'Upload')
),
FieldsValidator::create()
FormFieldsValidator::create()
);
return $form;
}
Expand Down

0 comments on commit fb02f4a

Please sign in to comment.