From dbd185cf40ab4eaea90d57692b82f9ce710cca2c Mon Sep 17 00:00:00 2001 From: HaoLiang Date: Wed, 7 Nov 2018 18:45:53 +0800 Subject: [PATCH 1/3] Fix Controller use validate bug Fixes #1419 --- system/Controller.php | 60 +++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/system/Controller.php b/system/Controller.php index ef1ba0448b33..65bb3b051c41 100644 --- a/system/Controller.php +++ b/system/Controller.php @@ -27,18 +27,19 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * - * @package CodeIgniter - * @author CodeIgniter Dev Team - * @copyright 2014-2018 British Columbia Institute of Technology (https://bcit.ca/) - * @license https://opensource.org/licenses/MIT MIT License - * @link https://codeigniter.com - * @since Version 3.0.0 + * @package CodeIgniter + * @author CodeIgniter Dev Team + * @copyright 2014-2018 British Columbia Institute of Technology (https://bcit.ca/) + * @license https://opensource.org/licenses/MIT MIT License + * @link https://codeigniter.com + * @since Version 3.0.0 * @filesource */ + use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; -use CodeIgniter\Log\Logger; use CodeIgniter\Validation\Validation; +use CodeIgniter\Validation\Exceptions\ValidationException; use Psr\Log\LoggerInterface; /** @@ -75,6 +76,7 @@ class Controller /** * Instance of logger to use. + * * @var Log\Logger */ protected $logger; @@ -83,7 +85,7 @@ class Controller * Whether HTTPS access should be enforced * for all methods in this controller. * - * @var int Number of seconds to set HSTS header + * @var integer Number of seconds to set HSTS header */ protected $forceHTTPS = 0; @@ -108,9 +110,9 @@ class Controller */ public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { - $this->request = $request; + $this->request = $request; $this->response = $response; - $this->logger = $logger; + $this->logger = $logger; $this->logger->info('Controller "' . get_class($this) . '" loaded.'); if ($this->forceHTTPS > 0) @@ -129,9 +131,9 @@ public function initController(RequestInterface $request, ResponseInterface $res * will happen back to this method and HSTS header will be sent * to have modern browsers transform requests automatically. * - * @param int $duration The number of seconds this link should be - * considered secure for. Only with HSTS header. - * Default value is 1 year. + * @param integer $duration The number of seconds this link should be + * considered secure for. Only with HSTS header. + * Default value is 1 year. * * @throws \CodeIgniter\HTTP\RedirectException */ @@ -146,7 +148,7 @@ public function forceHTTPS(int $duration = 31536000) * Provides a simple way to tie into the main CodeIgniter class * and tell it how long to cache the current page for. * - * @param int $time + * @param integer $time */ public function cachePage(int $time) { @@ -161,7 +163,9 @@ public function cachePage(int $time) protected function loadHelpers() { if (empty($this->helpers)) + { return; + } foreach ($this->helpers as $helper) { @@ -175,15 +179,37 @@ protected function loadHelpers() * A shortcut to performing validation on input data. If validation * is not successful, a $errors property will be set on this class. * - * @param array $rules - * @param array $messages An array of custom error messages + * @param array $rules + * @param array $messages An array of custom error messages * - * @return bool + * @return boolean */ public function validate($rules, array $messages = []): bool { $this->validator = Services::validation(); + // If you replace the $rules array with the name of the group + if (is_string($rules)) + { + $validation = new \Config\Validation(); + + // If the rule wasn't found in the \Config\Validation, we + // should throw an exception so the developer can find it. + if (! isset($validation->$rules)) + { + throw ValidationException::forRuleNotFound($rules); + } + + // If no error message is defined, use the error message in the Config\Validation file + if (count($messages) === 0) + { + $errorName = $rules . '_errors'; + $messages = $validation->$errorName ?? []; + } + + $rules = $validation->$rules; + } + $success = $this->validator ->withRequest($this->request) ->setRules($rules, $messages) From b885f53fe2f8a42e41fe5a4949906d53ae98ab10 Mon Sep 17 00:00:00 2001 From: HaoLiang Date: Wed, 7 Nov 2018 18:53:19 +0800 Subject: [PATCH 2/3] Fix user guide error for controller. Fixes #1419 --- user_guide_src/source/incoming/controllers.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/incoming/controllers.rst b/user_guide_src/source/incoming/controllers.rst index 838e1219fc45..93e3c004ce5b 100644 --- a/user_guide_src/source/incoming/controllers.rst +++ b/user_guide_src/source/incoming/controllers.rst @@ -323,7 +323,7 @@ has details on the format of the rules and messages arrays, as well as available ])) { return view('users/update', [ - 'errors' => $this->errors + 'errors' => $this->validator->getErrors() ]); } @@ -338,7 +338,7 @@ name of the group, as defined in ``Config\Validation.php``:: if (! $this->validate('userRules')) { return view('users/update', [ - 'errors' => $this->errors + 'errors' => $this->validator->getErrors() ]); } From 9c28bcf4344c682a4fe41f2110571fb4b65f7b68 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 8 Nov 2018 08:49:42 +0800 Subject: [PATCH 3/3] if (! $messages) for better performance Co-Authored-By: bangbangda --- system/Controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Controller.php b/system/Controller.php index 65bb3b051c41..c0016fe50217 100644 --- a/system/Controller.php +++ b/system/Controller.php @@ -201,7 +201,7 @@ public function validate($rules, array $messages = []): bool } // If no error message is defined, use the error message in the Config\Validation file - if (count($messages) === 0) + if (! $messages) { $errorName = $rules . '_errors'; $messages = $validation->$errorName ?? [];