From 6837c9dfab9e58c5fe3399b717899da30d78fc10 Mon Sep 17 00:00:00 2001 From: Julius Ijie Date: Fri, 23 Feb 2018 15:25:35 +0100 Subject: [PATCH 1/3] Added option to stop validation when a rule is failed --- src/Valitron/Validator.php | 19 +++++++++++++++++ tests/Valitron/StopOnFirstFailTest.php | 28 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/Valitron/StopOnFirstFailTest.php diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index b45b69a..6952586 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -77,6 +77,11 @@ class Validator */ protected $validUrlPrefixes = array('http://', 'https://', 'ftp://'); + /** + * @var bool + */ + protected $stop_on_first_fail = false; + /** * Setup validation * @@ -965,6 +970,7 @@ class Validator */ public function validate() { + $set_to_break = false; foreach ($this->_validations as $v) { foreach ($v['fields'] as $field) { list($values, $multiple) = $this->getPart($this->_fields, explode('.', $field)); @@ -999,13 +1005,26 @@ class Validator if (!$result) { $this->error($field, $v['message'], $v['params']); + if($this->stop_on_first_fail) { + $set_to_break = true; + break; + } } } + if($set_to_break) break; } return count($this->errors()) === 0; } + /** + * Should the validation stop a rule is failed + * @param bool $stop + */ + public function stopOnFirstFail(bool $stop) { + $this->stop_on_first_fail = $stop; + } + /** * Returns all rule callbacks, the static and instance ones. * diff --git a/tests/Valitron/StopOnFirstFailTest.php b/tests/Valitron/StopOnFirstFailTest.php new file mode 100644 index 0000000..656427d --- /dev/null +++ b/tests/Valitron/StopOnFirstFailTest.php @@ -0,0 +1,28 @@ + array( + array('lengthMin', 5, 'message'=>'myField1 must be 5 characters minimum'), + array('url', 'message' => 'myField1 is not a valid url'), + array('urlActive', 'message' => 'myField1 is not an active url') + ) + ); + + $v = new Validator(array( + 'myField1' => 'myVal' + )); + + $v->mapFieldsRules($rules); + $v->stopOnFirstFail(true); + $this->assertFalse($v->validate()); + + $errors = $v->errors(); + $this->assertCount(1, $errors['myField1']); + } + +} From 8636fed8f5e1e0fac841e604b545c7db4d42ac9d Mon Sep 17 00:00:00 2001 From: Julius Ijie Date: Fri, 23 Feb 2018 15:55:45 +0100 Subject: [PATCH 2/3] Removed boolean type declaration only available on PHP7 --- src/Valitron/Validator.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 6952586..74e41c6 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -1021,7 +1021,8 @@ class Validator * Should the validation stop a rule is failed * @param bool $stop */ - public function stopOnFirstFail(bool $stop) { + public function stopOnFirstFail($stop) { + if(!is_bool($stop)) throw new \InvalidArgumentException('Parameter of type boolean expected'); $this->stop_on_first_fail = $stop; } From 4103c8bd40a6b19296c41b8ac6571fd34ce95108 Mon Sep 17 00:00:00 2001 From: Julius Ijie Date: Fri, 23 Feb 2018 15:59:30 +0100 Subject: [PATCH 3/3] Forced stopOnFirstFail parameter to bool --- src/Valitron/Validator.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 74e41c6..d9fe2f7 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -1021,9 +1021,8 @@ class Validator * Should the validation stop a rule is failed * @param bool $stop */ - public function stopOnFirstFail($stop) { - if(!is_bool($stop)) throw new \InvalidArgumentException('Parameter of type boolean expected'); - $this->stop_on_first_fail = $stop; + public function stopOnFirstFail($stop = true) { + $this->stop_on_first_fail = (bool) $stop; } /**