Skip to content

Commit

Permalink
feat: return only validated content
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Nov 8, 2024
1 parent d7a43c8 commit c32c295
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Form
'max' => '{Field} must not exceed %s characters',
'between' => '{Field} must be between %s and %s characters long',
'match' => '{Field} must match the %s field',
'matchesvalueof' => '{Field} must match the value of %s',
'contains' => '{Field} must contain %s',
'boolean' => '{Field} must be a boolean',
'truefalse' => '{Field} must be a boolean',
Expand Down Expand Up @@ -132,7 +133,6 @@ public function __construct()
};

$this->rules['matchesvalueof'] = function ($value, $param) {
$this->message('matchesvalueof', "{field} must match the value of $param");
return \Leaf\Http\Request::get($param) === $value;
};
}
Expand Down Expand Up @@ -271,17 +271,23 @@ public function validate(array $dataSource, array $validationSet)
// clear previous errors
$this->errors = [];

$output = $dataSource;
$output = [];

foreach ($validationSet as $itemToValidate => $userRules) {
if (empty($userRules)) {
$output[$itemToValidate] = Anchor::deepGetDot($dataSource, $itemToValidate);
continue;
}

$endsWithWildcard = substr($itemToValidate, -1) === '*';
$itemToValidate = $endsWithWildcard ? substr($itemToValidate, 0, -1) : $itemToValidate;

$value = Anchor::deepGetDot($dataSource, $itemToValidate);

if (!$this->test($userRules, $value, $itemToValidate)) {
$output = false;
} else if ($output !== false && !$endsWithWildcard) {
$output = Anchor::deepSetDot($output, $itemToValidate, $value);
}
}

Expand Down

0 comments on commit c32c295

Please sign in to comment.