1

我正在运行 codeigniter 4.1.5 和 php 8.1.0,当我尝试输入数据时,这是我得到的错误

未捕获的错误异常:strtolower():将 null 传递给字符串类型的参数 #1 ($string) 在 C:\xampp\htdocs\cikaryawan\system\Validation\FormatRules.php:253 中已弃用 堆栈跟踪:

  1. #0 [内部函数]: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【内部函数】:CodeIgniter\Debug\Exceptions->exceptionHandler()
  9. #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 =

这是我的 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>

这是我作为控制器的 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');
            }
        }
    }
}

这就是我在 helpers 文件夹中放入 form_helper.php 的内容

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

我做错了什么?

4

1 回答 1