Welcome Guest, Not a member yet? Register   Sign In
Form Validation not Working 4.2.12 -> 4.3.1
#1

(This post was last modified: 01-15-2023, 08:12 AM by kodokrawa.)

Need help
After Update 4.2.12 to 4.3.1
Validation error not working, is there code need to change?
Because error validation not show
Please

Controler BaseController.php
PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
Psr\Log\LoggerInterface;
use 
CodeIgniter\HTTP\CLIRequest;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
/**
 * 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.
 */
abstract class BaseController extends Controller
{
    /**
    * Instance of the main Request object.
    *
    * @var CLIRequest|IncomingRequest
    */
    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 = [];

    /**
    * Constructor.
    */
    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();

        $this->validation = \Config\Services::validation(); //Undefined property '$validation'.intelephense(1014)
        $this->session = \Config\Services::session(); //Undefined property '$session'.intelephense(1014)

    }




Controller Login.php
PHP Code:
class Login extends BaseController
{

    public function index()
    {
        $data['validation'] = \Config\Services::validation();
        return view('login'$data);
    }
.... 

PHP Code:
public function ceklogin()
    {

        if (!$this->validate([
            'wa' => [
                'rules' => 'trim|required',
                'errors' => [
                    'required' => 'Nomor Whatsapp harus diisi',
                ]
            ],
            'password' => [
                'rules' => 'trim|required',
                'errors' => [
                    'required' => 'Password harus diisi',
                ]
            ],

        ])) {
            
            
return redirect()->to('login')->withInput();
        }
  }



My View login_view.php
PHP Code:
<div class="mb-3">
                        <label for="wa" class="form-label m-0">Nomor Whatsapp</label>
                        <input type="number" class="form-control <?= ($validation->hasError('wa')) ? 'is-invalid'  : ''; ?>" name="wa" id="wa" aria-describedby="wa"  autocomplete="off">
                        <div class="invalid-feedback">
                            <?= $validation->getError('wa'); ?>
                        </div>
                    </div> 
Reply
#2

Yes, you need to change the code.
See https://codeigniter4.github.io/CodeIgnit...ion-errors
Reply
#3

(01-15-2023, 05:27 PM)kenjis Wrote: Yes, you need to change the code.
See https://codeigniter4.github.io/CodeIgnit...ion-errors

Thank You it works
but value="<?= old('field_name')?>" not return the old value after error message 

how to resolve this Sir?
Reply
#4

old() did not change. It should work as before.
How do you use it? Sample code?
Reply
#5

(01-16-2023, 08:02 PM)kenjis Wrote: old() did not change. It should work as before.
How do you use it? Sample code?
My Controller Login.php
PHP Code:
public function ceklogin()
    {

        if (!$this->validate([
            'wa' => [
                'rules' => 'trim|required',
                'errors' => [
                    'required' => 'Nomor Whatsapp harus diisi',
                ]
            ],
            'password' => [
                'rules' => 'trim|required',
                'errors' => [
                    'required' => 'Password harus diisi',
                ]
            ],

        ])) {
            return view('login', ['validation' => $this->validator,]);
        }
.....
.....
.....


This is my View Page

Login.php
PHP Code:
<?php $validation = \Config\Services::validation(); ?>
                <form action="/cek-login" method="post">
                    <?= csrf_field() ?>
                    <div class="mb-3">
                        <label for="wa" class="form-label m-0">Nomor Whatsapp</label>
                        <input type="number" class="form-control  <?= ($validation->hasError('wa')) ? 'is-invalid'  ''?>" name="wa" id="wa" aria-describedby="wa" value="<?= old('wa'?>" autocomplete="off">
                        <div class="invalid-feedback">
                            <?= $error $validation->getError('wa'); ?>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label for="password" class="form-label m-0">Password</label>
                        <input type="password" class="form-control <?= ($validation->hasError('password')) ? 'is-invalid'  ''?>" name="password" id="password" placeholder="******" autocomplete="off">
                        <span class="mata"><i class="bi bi-eye-fill" id="show_eye"></i></span>
                        <div class="invalid-feedback">
                            <?= $error $validation->getError('password'); ?>
                        </div>
                    </div>
                    <button type="submit" class="btn btn-kendaraan w-100 mb-2">Login</button>
                </form> 

When Validation is-invalid
old input value not show
Reply
#6

old() needs redirect()->withInput() in the previous page(request).
See https://codeigniter.com/user_guide/gener...s.html#old

I think your code does not work in v4.2.

You need to use Form helper set_value().
See https://github.com/kenjis/ci4-validation-tutorial
Reply
#7

(01-18-2023, 06:10 PM)kenjis Wrote: old() needs redirect()->withInput() in the previous page(request).
See https://codeigniter.com/user_guide/gener...s.html#old

I think your code does not work in v4.2.

You need to use Form helper set_value().
See https://github.com/kenjis/ci4-validation-tutorial

Thanks you Sir, its Works
Reply
#8

my code not works yet, can you provide a view code that showing an errors?
Reply
#9

See https://github.com/kenjis/ci4-validation.../form2.php
Reply




Theme © iAndrew 2016 - Forum software by © MyBB