codeigniter 未捕获的错误异常:字符串下限():不推荐将null传递给string类型的参数#1($string)

u3r8eeie  于 2023-02-06  发布在  其他
关注(0)|答案(2)|浏览(144)

我正在运行codeigniter 4.1.5和php 8.1.0,当我尝试输入数据时,这是我得到的错误
未捕获的错误异常:字符串下限():在C:\xampp\htdocs\cikaryawan\system\Validation\FormatRules中不推荐将null传递给字符串类型的参数#1($string)。php:253堆栈跟踪:
1.#0 [内部功能]:代码触发器\调试\异常-〉错误处理程序()
1.#1 C:\系统\验证\格式规则. php(253):strtolower(字符串)
1.第二个C:\系统\HTTP\请求特征. php(151):代码触发器\验证\格式规则-〉valid_ip()
1.#3 C:\xampp\htdocs\cikaryawan\app\视图\错误\html\错误_异常. php(206):代码触发器\HTTP\请求-〉获取IP地址()
1.#4 C:\系统\调试\异常. php(229):包含('...')
1.#5 C:\系统\调试\异常. php(232):代码触发器\调试\异常-〉代码触发器\调试{闭包}()
1.#6 C:\系统\调试\异常. php(116):代码触发器\调试\异常-〉render()
1.#7 [内部功能]:代码触发器\调试\异常-〉异常处理程序()
1.#8 {main}在我的. env文件夹中

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 =

这是我的注册表

<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>

这是我作为控制器的auth.php

<?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');
            }
        }
    }
}

这是我在form_helper.php中放入的内容

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

我做错了什么?

wj8zmpe1

wj8zmpe11#

CodeIgniter 4.1.5还不支持PHP 8.1。请使用8.0
或使用CI4的develop分支
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

c9x0cxw0

c9x0cxw02#

您可以将空合并运算符添加到以下代码行:
$方法= strtolower($方法);将其改为:
$方法=低层($方法??"“);
此更改应在库/RestController文件第835行中进行

相关问题