| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Metricool\Support\Validation\Rules; |
| 6 |
|
| 7 |
class MatchesRule extends AbstractRule |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Checks if the field matches the value of another field, e.g. |
| 11 |
* "passwordConfirmation" => "matches:password". When no field is given it |
| 12 |
* falls back to "{field}_confirmation". |
| 13 |
* @inheritDoc |
| 14 |
*/ |
| 15 |
public function validate(string $field, $value, array $data): void |
| 16 |
{ |
| 17 |
$otherField = $this->parameters[0] ?? $field . '_confirmation'; |
| 18 |
|
| 19 |
if (!array_key_exists($otherField, $data) || $data[$otherField] !== $value) { |
| 20 |
$this->fail(sprintf( |
| 21 |
/* translators: %1$s and %2$s are field names */ |
| 22 |
__('%1$s must match %2$s.', 'metricool'), |
| 23 |
$field, |
| 24 |
$otherField |
| 25 |
)); |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|