From c32c2954b3609ab79217c9e6acca5ca8cf706f88 Mon Sep 17 00:00:00 2001 From: mychidarko Date: Fri, 8 Nov 2024 19:48:23 +0000 Subject: [PATCH] feat: return only validated content --- src/Form.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Form.php b/src/Form.php index d2dfc7f..57e73cb 100644 --- a/src/Form.php +++ b/src/Form.php @@ -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', @@ -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; }; } @@ -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); } }