From cdf2656f49aa32caac3b3f1b0230e9dca062c4dc Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 13 Jun 2022 23:34:29 +0300 Subject: [PATCH] Add length rule: "The :attribute must must have an exact length of :length" --- src/Rules/Length.php | 33 +++++++++++++++++++++++++++++++++ src/Validator.php | 1 + 2 files changed, 34 insertions(+) create mode 100644 src/Rules/Length.php diff --git a/src/Rules/Length.php b/src/Rules/Length.php new file mode 100644 index 0000000..66365e7 --- /dev/null +++ b/src/Rules/Length.php @@ -0,0 +1,33 @@ +requireParameters($this->fillableParams); + + $this->requireParameters($this->fillableParams); + + $length = (int) $this->parameter('length'); + + return strlen((string) $value) == $length; + } +} diff --git a/src/Validator.php b/src/Validator.php index dfa19c5..71e65ce 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -162,6 +162,7 @@ protected function registerBaseValidators() 'defaults' => new Rules\Defaults, 'default' => new Rules\Defaults, // alias of defaults 'nullable' => new Rules\Nullable, + 'length' => new Rules\Length, ]; foreach ($baseValidator as $key => $validator) {