-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Because of how the validation engine works, there's no reason to keep adding templates to each rule. Instead, creating a single rule that handles templating rules will simplify the library greatly and shrink the `Rule` interface. Personally, I think this API is much more straightforward than the `setTemplate()` method, as it's way more explicit which rule is being templated.
- Loading branch information
1 parent
634a155
commit 6a33f16
Showing
25 changed files
with
239 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Templated | ||
|
||
- `Templated(Rule $rule, string $template)` | ||
- `Templated(Rule $rule, string $template, array<string, mixed> $parameters)` | ||
|
||
Defines a rule with a custom message template. | ||
|
||
```php | ||
v::templated(v::email(), 'You must provide a valid email to signup')->assert('not an email'); | ||
// Message: You must provide a valid email to signup | ||
|
||
v::templated( | ||
v::notEmpty(), | ||
'The author of the page {{title}} is empty, please fill it up.', | ||
['title' => 'Feature Guide'] | ||
)->assert(''); | ||
// Message: The author of the page "Feature Guide" is empty, please fill it up. | ||
``` | ||
|
||
This rule can be also useful when you're using [Attributes](Attributes.md) and want a custom template for a specific property. | ||
|
||
## Templates | ||
|
||
This rule does not have any templates, as you must define the templates yourself. | ||
|
||
## Template placeholders | ||
|
||
| Placeholder | Description | | ||
|-------------------|------------------------------------------------------------------| | ||
| `name` | The validated input or the custom validator name (if specified). | | ||
|
||
|
||
## Categorization | ||
|
||
- Core | ||
- Structures | ||
- Miscellaneous | ||
|
||
## Changelog | ||
|
||
| Version | Description | | ||
|--------:|-------------| | ||
| 3.0.0 | Created | | ||
|
||
*** | ||
See also: | ||
|
||
- [Attributes](Attributes.md) | ||
- [Not](Not.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Rules; | ||
|
||
use Attribute; | ||
use Respect\Validation\Result; | ||
use Respect\Validation\Rule; | ||
use Respect\Validation\Rules\Core\Wrapper; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] | ||
final class Templated extends Wrapper | ||
{ | ||
/** @param array<string, mixed> $parameters */ | ||
public function __construct( | ||
Rule $rule, | ||
private readonly string $template, | ||
private readonly array $parameters = [] | ||
) { | ||
parent::__construct($rule); | ||
} | ||
|
||
public function getTemplate(): string | ||
{ | ||
return $this->template; | ||
} | ||
|
||
public function evaluate(mixed $input): Result | ||
{ | ||
$result = $this->rule->evaluate($input); | ||
if (!$result->hasCustomTemplate()) { | ||
return $result->withTemplate($this->template)->withExtraParameters($this->parameters); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
Oops, something went wrong.