Skip to content

Commit

Permalink
Merge pull request #71 from neos/task/documentCustomValdators
Browse files Browse the repository at this point in the history
DOCS: Add tutorial about custom validators
  • Loading branch information
mficzel authored Dec 10, 2021
2 parents af3334b + 0e4aff8 commit b27f377
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
73 changes: 73 additions & 0 deletions Documentation/Tutorials/CustomValidators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Custom Validators

Custom Validators allow to implement specific rules and error messages for the submitted values.
Validators implement the `\Neos\Flow\Validation\Validator\ValidatorInterface` which is made comfortable with the
`\Neos\Flow\Validation\Validator\AbstractValidator` base class that reduces the effort to implementing a single
`isValid()` Method. The behavior of validators can be configured via options. All found errors passed to the
`addError()` method and will be combined with results other validators may return.

ATTENTION: The validator resolver searches validators in the namespace `__PackageNamespace__\Validation\Validator\__ValidatorName__Validator`
when addressed via `__PackageKey__:__ValidatorName__`.

Flow validation documentation: https://flowframework.readthedocs.io/en/stable/TheDefinitiveGuide/PartIII/Validation.html

```
<?php
declare(strict_types=1);
namespace Vendor\Site\Validation\Validator;
use Neos\Flow\Validation\Validator\AbstractValidator;
class AllowedValuesValidator extends AbstractValidator
{
/**
* @var boolean
*/
protected $acceptsEmptyValues = false;
/**
* @var mixed[]
*/
protected $supportedOptions = array(
'allowedValues' => array([], 'Array of allowed values', 'array', false),
);
/**
* @param mixed $value
* @return void
*/
protected function isValid($value)
{
if (!in_array( $value, $this->options['allowedValues'])) {
$this->addError('Some values are more correct than others.', 123456789);
}
}
}
```

The custom validator can now be used in fusion forms.

```
prototype(Vendor.Site:Content.FormExample) < prototype(Neos.Fusion.Form:Runtime.RuntimeForm) {
process {
content = afx`
<Neos.Fusion.Form:FieldContainer field.name="favoriteMovie" label="Favorite Movie">
<Neos.Fusion.Form:Input />
</Neos.Fusion.Form:FieldContainer>
`
schema {
#
# A custom validator is identified via PackageKey and ValidatorName seperated by a colon.
# The implementation is expected to be in the php namespace `__PackageNamespace__\Validation\Validator\__ValidatorName__Validator`
#
favoriteMovie = ${Form.Schema.string().validator('Vendor.Site:AllowedValues', {allowedValues: ['Star Wars', 'Simpsons']})}
}
}
...
```

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ be implemented like this.
- [Custom form action](Documentation/Tutorials/CustomFormAction.md)
- [Honeypot field](Documentation/Tutorials/HoneypotField.md)
- [Conditional fields and steps](Documentation/Tutorials/ConditionalFieldsAndSteps.md)
- [Custom Validators](Documentation/Tutorials/CustomValidators.md)
- Examples
- [SingleStepForm](Documentation/Examples/SingleStepForm.md)
- [MultiStepForm](Documentation/Examples/MultiStepForm.md)
Expand Down

0 comments on commit b27f377

Please sign in to comment.