Uncaught ErrorException: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated codeigniter
Asked Answered
C

2

9

I'm running codeigniter 4.1.5 and php 8.1.0 and when i try to input data this is the error i got

Uncaught ErrorException: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in C:\xampp\htdocs\cikaryawan\system\Validation\FormatRules.php:253 Stack trace:

  1. #0 [internal function]: CodeIgniter\Debug\Exceptions->errorHandler()
  2. #1 C:\xampp\htdocs\cikaryawan\system\Validation\FormatRules.php(253): strtolower()
  3. #2 C:\xampp\htdocs\cikaryawan\system\HTTP\RequestTrait.php(151): CodeIgniter\Validation\FormatRules->valid_ip()
  4. #3 C:\xampp\htdocs\cikaryawan\app\Views\errors\html\error_exception.php(206): CodeIgniter\HTTP\Request->getIPAddress()
  5. #4 C:\xampp\htdocs\cikaryawan\system\Debug\Exceptions.php(229): include('...')
  6. #5 C:\xampp\htdocs\cikaryawan\system\Debug\Exceptions.php(232): CodeIgniter\Debug\Exceptions->CodeIgniter\Debug{closure}()
  7. #6 C:\xampp\htdocs\cikaryawan\system\Debug\Exceptions.php(116): CodeIgniter\Debug\Exceptions->render()
  8. #7 [internal function]: CodeIgniter\Debug\Exceptions->exceptionHandler()
  9. #8 {main} in my .env folder
 CI_ENVIRONMENT = development
 database.default.hostname = localhost
 database.default.database = cikaryawanauth
 database.default.username = root
 database.default.password = 
 database.default.DBDriver = MySQLi
 database.default.DBPrefix =

this is my register.php

<body>

    <div class="container">
        <div class="row" style="margin-top:45px">
            <div class="col-md-4 col-md-offset-4">
                <h4>Sign Up</h4>
                <hr>
                <form action="<?= base_url('auth/save') ?>" method="post">
                    <?= csrf_field(); ?>
                    <div class="form-group">
                        <label for="">Name</label>
                        <input type="text" class="form-control" name="name" placeholder="Enter your name" value="<?= set_value('name'); ?>">
                        <span class="text-danger"><?= isset($validation) ? display_error($validation, 'name') : '' ?></span>
                    </div>
                    <div class="form-group">
                        <label for="">Email</label>
                        <input type="email" class="form-control" name="email" placeholder="Enter your email" value="<?= set_value('email'); ?>">
                        <span class=" text-danger"><?= isset($validation) ? display_error($validation, 'email') : '' ?></span>
                    </div>
                    <div class="form-group">
                        <label for="">Password</label>
                        <input type="password" class="form-control" name="password" placeholder="Enter password">
                        <span class="text-danger"><?= isset($validation) ? display_error($validation, 'password') : '' ?></span>
                    </div>
                    <div class="form-group">
                        <label for="">Confirm Password</label>
                        <input type="password" class="form-control" name="cpassword" placeholder="Confirm password">
                        <span class="text-danger"><?= isset($validation) ? display_error($validation, 'cpassword') : '' ?></span>
                    </div>
                    <div class="form-group">
                        <button class="btn btn-primary btn-block" type="submit">Sign Up</button>
                    </div>
                    <a href="<?= site_url('auth/') ?>">I already have account, login now</a>
                </form>
            </div>
        </div>
    </div>

</body>

this is my auth.php as controller

<?php

namespace App\Controllers;

class Auth extends BaseController
{
    public function __construct()
    {
        helper(['url', 'form']);
    }
    public function index()
    {
        return view('auth/login');
    }
    public function register()
    {
        return view('auth/register');
    }
    public function save()
    {
        $validation = $this->validate([
            'name' => [
                'rules' => 'required',
                'errors' => [
                    'required' => 'Your name is required'
                ]
            ],
            'email' => [
                'rules' => 'required|valid_emails|is_unique[users.email]',
                'errors' => [
                    'required' => 'Email is required',
                    'valid_email' => 'You must enter a valid email',
                    'is_unique' => 'Email already taken'
                ]
            ],
            'password' => [
                'rules' => 'required|min_length[5]|max_length[12]',
                'errors' => [
                    'required' => 'Password is required',
                    'min_lenght' => 'Password must have atleast 5 character',
                    'max_lenght' => 'Password must not exceed 12 character'
                ]
            ],
            'cpassword' => [
                'rules' => 'required|min_length[5]|max_length[12]|matches[password]',
                'errors' => [
                    'required' => 'Password is required',
                    'min_lenght' => 'Password must have atleast 5 character',
                    'max_lenght' => 'Password must not exceed 12 character',
                    'matches' => 'Confirms password not matches to password'
                ]
            ]
        ]);

        if (!$validation) {
            return view('auth/register', ['validation' => $this->validator]);
        } else {
            $name = $this->request->getPost('name');
            $email = $this->request->getPost('email');
            $password = $this->request->getPost('password');

            $values = [
                'name' => $name,
                'email' => $email,
                'password' => $password,
            ];
            $userModels = new \App\Models\UsersModel();
            $query = $userModels->insert($values);
            if (!$query) {
                return redirect()->back()->with('fail', 'something went wrong');
            } else {
                return redirect()->to('register')->with('success', 'You are now registered');
            }
        }
    }
}

this is what i put in form_helper.php in helpers folder

<?php
function display_error($validation, $field)
{
    if ($validation->hasError($field)) {
        return $validation->getError($field);
    } else {
        return false;
    }
}

what did i do wrong?

Congregationalism answered 9/12, 2021 at 5:24 Comment(2)
Can you tell us which line is Validation\FormatRules.php:253?Dais
it seems to be a bug - this should explain your problem: forum.codeigniter.com/thread-80490-post-391352.html#pid391352: The function incorrectly accepts NULL as a string and should be replaced with an empty string., try to upgrade to latest CI 4.xSlight
A
12

You can add the null coalescing operator to this line of code:

$method = strtolower($method);

Replace it with:

$method = strtolower($method ?? '');

This change should be made in the library/RestController file line no 835

Azikiwe answered 3/2, 2023 at 11:34 Comment(0)
T
4

CodeIgniter 4.1.5 does not support PHP 8.1 yet. Please Use 8.0

or Use develop branch of CI4

https://github.com/codeigniter4/CodeIgniter4/pull/4883

https://github.com/codeigniter4/CodeIgniter4/issues/5436

https://forum.codeigniter.com/thread-80490-post-391352.html#pid391352

https://forum.codeigniter.com/thread-80413.html

Thyroid answered 10/12, 2021 at 18:28 Comment(1)
it seems to be a bug, which can easily be fixed: The function incorrectly accepts NULL as a string and should be replaced with an empty stringSlight

© 2022 - 2024 — McMap. All rights reserved.