From 695b4ac75a8f0bf3dbdeecd38305f68ce1f403c6 Mon Sep 17 00:00:00 2001 From: Alannah Kearney Date: Sat, 25 May 2019 11:59:29 +1000 Subject: [PATCH] Updated validation logic for inputs that have a validator, no default, and are not set. Using InputValidationFailedException exception when validation fails --- src/Input/AbstractInputHandler.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Input/AbstractInputHandler.php b/src/Input/AbstractInputHandler.php index 4861a68..cb1a799 100644 --- a/src/Input/AbstractInputHandler.php +++ b/src/Input/AbstractInputHandler.php @@ -45,13 +45,17 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface foreach($items as $input) { self::checkRequiredAndRequiredValue($input, $this->input); + // There is a default value, input has not been set, and there + // is no validator if( null !== $input->default() && null === $this->find($input->name()) && null === $input->validator() ) { $result = $input->default(); - } elseif(null !== $input->validator()) { + + // Input has been set and it has a validator + } elseif(null !== $this->find($input->name()) && null !== $input->validator()) { $validator = $input->validator(); if ($validator instanceof \Closure) { @@ -60,7 +64,13 @@ abstract class AbstractInputHandler implements Interfaces\InputHandlerInterface throw new \Exception("Validator for '{$input->name()}' must be NULL or an instance of either Closure or Input\Validator."); } - $result = $validator->validate($input, $this); + try { + $result = $validator->validate($input, $this); + } catch(\Exception $ex) { + throw new Exceptions\InputValidationFailedException($input, 0, $ex); + } + + // No default, no validator, but may or may not have been set } else { $result = $this->find($input->name()); }