Php 在Codeigniter中获取自定义错误消息数组

Php 在Codeigniter中获取自定义错误消息数组,php,codeigniter,Php,Codeigniter,只是想找出让自定义错误消息正常工作的最佳方法。Isset在codeigniter中似乎不起作用 出于某种原因,我的输入显示了数字'1' 控制器 <?php class Step_3 extends MX_Controller { private $error = array(); public function index() { if(($this->input->server('REQUEST_METHOD') == 'POST') && $th

只是想找出让自定义错误消息正常工作的最佳方法。Isset在codeigniter中似乎不起作用

出于某种原因,我的输入显示了数字
'1'

控制器

<?php

class Step_3 extends MX_Controller { 

private $error = array();

public function index() {
  if(($this->input->server('REQUEST_METHOD') == 'POST') && $this->validate()) {
    $data['db_hostname']    = $this->input->post('db_hostname');
  }

  if ($this->error['db_hostname']) {
    $data['error_db_hostname'] = $this->error['db_hostname'];
  } else {
    $data['error_db_hostname'] = '';
  }

  if ($this->input->post('db_hostname')) {
    $data['db_hostname'] = $this->input->post('db_hostname');
  } else {
    $data['db_hostname'] = 'localhost';
  }
}

private function validate() {
  if (!$this->input->post('db_hostname')) {
    $this->error['db_hostname'] = 'Hostname required!';
  }
}

} // End Of

您的“validate”函数不会返回任何内容,因此您的第一条if语句永远不会为true。 同时,此时输入将自动具有“localhost”值。最好测试空字符串和带空格,以防出现以下情况:

if ("" == trim($this->input->post('db_hostname'))) 

这个区域怎么办?$this->error['db\u hostname']无法设置isset如果您显式地给error['db\u hostname']指定一个值,然后使用
(isset($this->error['db\u hostname'))
它的计算结果正确吗?没有人一直说不能使用isset我尝试了null==消息现在显示,但我已再次尝试,但现在说未定义的索引:db\u localhost bug在这里$this->error['db\u hostname']尝试使用array\u key\u exists
if ("" == trim($this->input->post('db_hostname')))