Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Controller use validate bug Fixes #1419 #1423

Merged
merged 3 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 43 additions & 17 deletions system/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -75,6 +76,7 @@ class Controller

/**
* Instance of logger to use.
*
* @var Log\Logger
*/
protected $logger;
Expand All @@ -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;

Expand All @@ -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)
Expand All @@ -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
*/
Expand All @@ -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)
{
Expand All @@ -161,7 +163,9 @@ public function cachePage(int $time)
protected function loadHelpers()
{
if (empty($this->helpers))
{
return;
}

foreach ($this->helpers as $helper)
{
Expand All @@ -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 (! $messages)
{
$errorName = $rules . '_errors';
$messages = $validation->$errorName ?? [];
}

$rules = $validation->$rules;
}

$success = $this->validator
->withRequest($this->request)
->setRules($rules, $messages)
Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/incoming/controllers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
]);
}

Expand All @@ -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()
]);
}

Expand Down