Codeigniter CI_Form_validation未定义的函数

时间:2014-09-09 05:07:27

标签: php forms codeigniter validation

所以我收到以下错误。

Fatal error: Call to undefined method CI_Form_validation::error_array() in /home/kmgpdev/public_html/projects/lm/application/controllers/api.php on line 64

第63到66行读取

if($this->form_validation->run() == false) {
    $this->output->set_output(json_encode(['result' => 0, 'error' => $this->form_validation->error_array()]));
    return false;
}

如果我删除第64行它工作正常,只是没有产生错误。 这里还有我作为自定义库创建的MY_Form_validation.php文件。

class MY_Form_validation extends CI_Form_validation
{
    public function __construct($config = array())
    {
        parent::__construct($config);

    }

    public function error_array()
    {
        if(count($this->_error_array > 0)) {
            return $this->_error_array;
        }
    }
}

所以它在localhost,xampp运行良好,当我上传到我的ubuntu服务器然后发生这个错误,我无法弄清楚为什么会出现这个错误。我正在使用php 5.5,有什么建议吗? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

要获取第一条错误消息,我已将这样的实用程序函数直接放在//system/libraries/Form_validation.php下。否则,您可以使用$ this-> form_validation-> error_string()而不是直接选择错误数组。在大多数情况下,您希望用户将错误视为字符串:

function first_error_string($prefix = '', $suffix = '')
{
    // No errrors, validation passes!
    if (count($this->_error_array) === 0)
    {
        return '';
    }

    if ($prefix == '')
    {
        $prefix = $this->_error_prefix;
    }

    if ($suffix == '')
    {
        $suffix = $this->_error_suffix;
    }

    // Generate the error string
    $str = '';
    foreach ($this->_error_array as $val)
    {
        if ($val != '')
        {
            $str .= $prefix.$val.$suffix;
            break;
        }
    }

    return $str;
}