【问题标题】:Codeigniter 3.1.9 MY_Form_validation is not workingCodeigniter 3.1.9 MY_Form_validation 不起作用
【发布时间】:2018-12-12 10:34:11
【问题描述】:

我正在使用 Codeigniter 3.1.9 并在本地计算机上完成了我的表单。我刚刚在服务器上上传了我的应用程序并收到错误

无法访问与您的字段名称对应的错误消息 网址。(有效网址格式)

我google了很多,但无法解决问题。

文件名:My_Form_validation.php 位置:应用程序\库

    class MY_Form_validation extends CI_Form_validation{

   public function __construct()
   {
     parent::__construct();
    }                           

    function valid_url_format($str){
        $pattern = "/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i";
                if (!preg_match($pattern, $str)){
            $this->set_message('valid_url_format', 'The URL you entered is not correctly formatted.');
            return FALSE;
        }

        return TRUE;
    }        

    function url_exists($url){                                   
        $url_data = parse_url($url); // scheme, host, port, path, query
        if(!fsockopen($url_data['host'], isset($url_data['port']) ? $url_data['port'] : 80)){
            $this->set_message('url_exists', 'The URL you entered is not accessible.');
            return FALSE;
        }               

        return TRUE;
    }  
}

文件名:UrlChecker.php 位置:应用程序\控制器

        class UrlChecker extends CI_Controller {

        public function __construct() {

            parent::__construct();

        }

        public  function _initializing(){



        }

        public function index()
        {



            $this->form_validation->set_rules('link', 'URL', 'required|trim|valid_url_format|url_exists');

            if ($this->form_validation->run() == FALSE)
            {
                echo validation_errors('<div class="alert alert-danger" role="alert">', '</div>');
            }

            else

            {

    echo 'ok';

    }

}

请检查并让我知道是托管版本问题还是其他问题。

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    我总是即时使用自定义验证,这是我第一次尝试制作自定义库以进行额外验证,无论如何我创建并测试了它以确保它有效,您必须确保遵循命名约定,文件名应该是这样的:MY_Form_validation.php 并将其保存在您的application/libraries 中,然后创建您的类:

    class MY_Form_validation extends CI_Form_validation
    {
        // your rules
    }
    

    然后您必须为每个方法创建错误消息,在 application/language/english/form_validation_lang.php 中创建一个 lang 文件并添加您的自定义错误消息,如下所示:

    $lang['valid_url_format'] = 'The {field} field may only contain valid url.';
    $lang['url_exists'] = 'The {field} field already exists';
    

    【讨论】:

    • 我做了同样的事情,你可以检查我已经解释了路径和全班提及,但没有运气
    • 它在本地主机上工作正常,但当我在实时服务器上上传时不工作
    • 您是按原样使用 CI 还是使用 hmvc 扩展?
    • 我使用 CI 刚刚从 codeigniter.com 3.1.9 版下载。
    • 您在控制器/模型或自动加载中加载了form_validation
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 2018-12-25
    • 1970-01-01
    • 2015-02-26
    • 2014-12-16
    • 1970-01-01
    相关资源
    最近更新 更多