Codeigniter -调用未定义的函数set_value()

6yjfywim  于 2022-12-07  发布在  其他
关注(0)|答案(5)|浏览(111)

我是新的codeigniter和尝试解决以下问题几个小时了。我试图在一个表单中设置_value,但当我设置它时,我得到以下错误:

  • 遇到PHP错误严重性:错误消息:调用未定义的函数set_value()文件名:第一行代码:27回溯:*

当我删除第27行的set_value()时,一切都正常。
第27行是set_value()行:

<div class="row"> 
 <div class="form-group">
  <label class="control-label sr-only">Vorname:</label>
   <div class="col-md-8 col-md-offset-2 col-xs-10 col-xs-offset-1">
   <input type="text" class="form-control" name="firstname" placeholder="Vorname" value="<?php echo set_value('firstname'); ?>" size="50" />
   <i class="fa form-control-feedback" aria-hidden="true"></i>
   </div>
  </div>
</div>

这是我的控制器:

class Form extends CI_Controller {

        public function index()
        {
                $this->load->helper('form');

                $this->load->library('form_validation');

                $this->form_validation->set_rules('firstname', 'Vorname', 'required|callback_username_check');
                $this->form_validation->set_rules('surename', 'Nachname', 'required');
                $this->form_validation->set_rules('email', 'Email', 'required');
                $this->form_validation->set_rules('password', 'Passwort', 'required');

                if ($this->form_validation->run() == FALSE)
                {
                        $this->load->view('modals/register_modal');
                }
                else
                {
                        $this->load->view('modals/login_modal');
                }
        }

        public function username_check($str)
        {
                if ($str == 'test')
                {
                        $this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
                        return FALSE;
                }
                else
                {
                        return TRUE;
                }
        }

}

我还设置了自动加载:

$autoload['helper'] = array('form');
smdnsysy

smdnsysy1#

你必须在你的控制器中加载library('form_validation')

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller{

    public function __construct(){
        parent::__construct();
        $this -> load -> library('form_validation');
    }
....
brtdzjyr

brtdzjyr2#

在CodeIgniter4中,只需使用 helper 函数来加载库

<?php namespace App\Controllers;
use App\Models\UserModel;
class Users extends BaseController {
public function index()
{
    helper (['form']);   
    ......
vfwfrxfs

vfwfrxfs3#

添加帮助器HTML

protected $helpers = ['form','url','html'];

对我很有效

6yoyoihd

6yoyoihd4#

您可以在BaseController.php中添加表单助手。您不需要在每个控件中添加- codeigniter 4
在BaseController.php中编辑

protected $helpers = ['form'];
hzbexzde

hzbexzde5#

helper(['form']);

在函数中加载表单帮助器

相关问题