Skip to content

Commit

Permalink
feat: add matchesValueOf + fix rule params
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Nov 8, 2024
1 parent 9138d7d commit d7a43c8
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public function __construct()
$this->rules['in'] = function ($value, $param) {
return in_array($value, $param);
};

$this->rules['matchesvalueof'] = function ($value, $param) {
$this->message('matchesvalueof', "{field} must match the value of $param");
return \Leaf\Http\Request::get($param) === $value;
};
}

protected function test($rule, $valueToTest, $fieldName = 'item'): bool
Expand Down Expand Up @@ -192,6 +197,10 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool
$param = ['Item'];
}

if (!is_array($param)) {
$param = [$param];
}

$this->addError($fieldName, sprintf(
str_replace(
['{field}', '{Field}', '{value}'],
Expand Down Expand Up @@ -313,10 +322,10 @@ public function addErrorMessage($field, ?string $message = null)
}

/**
* Add validation error message
* @param string|array $field The field to add the message to
* @param string|null $message The error message if $field is a string
*/
* Add validation error message
* @param string|array $field The field to add the message to
* @param string|null $message The error message if $field is a string
*/
public function addMessage($field, ?string $message = null)
{
if (is_array($field)) {
Expand Down

0 comments on commit d7a43c8

Please sign in to comment.