Welcome Guest, Not a member yet? Register   Sign In
Call to undefined function validation ()
#1

I am converting to codeigniter 4
In a sub view I want to show all the errors of the form. In codeigniter 3 I was using validator_errors without problems

In the BaseController I loaded the Validation livery


PHP Code:
$this->validation = \Config\Services::validation (); 

In the subview it is, while the errors see exist:

PHP Code:
if (! empty (validation () -> listErrors ()))
    echo validation () -> listErrors 


I get the error


Code:
Call to undefined function validation ()
Reply
#2

If you want to show validation errors in a view, you need to pass validation() as a variable to the view and then call it  like
Code:
<?= $validation->listErrors() ?>
https://codeigniter.com/user_guide/libra...n-tutorial
Reply
#3

(This post was last modified: 06-25-2021, 05:55 AM by lucavalentino.)

I tried as suggested but still get the same error .

In the BaseController I loaded the Validation library
$validation = \Config\Services::validation ();

I want to autoload the library validation
Reply
#4

Base Controller
PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\CLIRequest;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Psr\Log\LoggerInterface;


/**
* Class BaseController
*
* BaseController provides a convenient place for loading components
* and performing functions that are needed by all your controllers.
* Extend this class in any new controllers:
*    class Home extends BaseController
*
* For security be sure to declare any new methods as protected or private.
*/

class BaseController extends Controller
{
/**
* Instance of the main Request object.
*
* @var IncomingRequest|CLIRequest
*/
protected $request;

/**
* An array of helpers to be loaded automatically upon
* class instantiation. These helpers will be available
* to all other controllers that extend BaseController.
*
* @var array
*/
protected $helpers = ['my_redirect''url'];

/**
* Constructor.
*
* @param RequestInterface  $request
* @param ResponseInterface $response
* @param LoggerInterface  $logger
*/
public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request$response$logger);

//--------------------------------------------------------------------
// Preload any models, libraries, etc, here.
//--------------------------------------------------------------------
// E.g.: 
$this->session = \Config\Services::session();
$validation =  \Config\Services::validation();

}


Login controller
<?php


PHP Code:
namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
Login extends BaseController
{
public function 
index()
{
$data=['title'=>'login''menu'=>'si'];
return 
view('login_v'$data);
}



Login view
...
PHP Code:
$this->include('_mesages_v'$data); 
...
_mesages_v view
PHP Code:
...
 if(!empty(
$validation->listErrors()))

  echo validation()->listErrors();
... 

ERROR Undefined variable $validation
Reply
#5

Looks like you aren't sending $validation to the view, that would explain the undefined variable message.

File: _messages_v
Reply
#6

$data['validation'] = $this-validation;
view('login', $data);


// view
if ($validation->listErrors())
Reply
#7

(This post was last modified: 06-26-2021, 03:51 PM by Gary.)

$this->validation and $validation likely point to different things in your case.

Try using $this-> in all cases to reference the current instance of it... or using a local variable as a handle to the service that is then passed $this instance:

Code:
$validation = \Config\Services::validation();
...
$validationResult = $this->validate('validation_test');   // bool result

if ($validationResult) {
   // no validation errors
} else
   $validationErrors = $validation->getErrors();
// or $validation->getError('validation_test_specific_test_error');


Can be called in the View, if desired:
  echo $validation->getError('validation_test_specific_test_error');
Reply




Theme © iAndrew 2016 - Forum software by © MyBB