【问题标题】:codeigniter3 form validation callback function calls unnecessarilycodeigniter3 表单验证回调函数不必要地调用
【发布时间】:2017-02-07 12:26:23
【问题描述】:

我正在使用 codeigniter 3。我在表单中有一个电子邮件字段并在电子邮件上使用required|callback_is_email_exist rules当我将电子邮件字段留空时,它显示回调消息而不是必需的消息。 我曾在 codeigniter 2 上工作过,它完美地显示“必需”消息,CI3 表单验证在使用回调时未按顺序执行规则。 以下是我的代码


查看:welcome_message

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Welcome to CodeIgniter</title>
    </head>
    <body>
    <?php echo validation_errors(); ?>

    <?php echo form_open('Welcome'); ?>

    <h5>Username</h5>

    <input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />

    <div><input type="submit" value="Submit" /></div>
    </form>
    </body>
    </html>

控制器:welcome.php

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


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

        $this->form_validation->set_rules('email', 'Email', 'required|callback_is_email_exist');

                if ($this->form_validation->run() == FALSE)
                {
                     $this->load->view('welcome_message');
                }
                else
                {
                        echo 'success';
                }
    }

         public function is_email_exist($str)
        {
            //  code to check email exist in databse here
                if (is_email_exist($str)
                {
                      return TRUE;  
                }
                else
                { 
                $this->form_validation->set_message('is_email_exist', 'Email Does not exist');
                            return FALSE;
                    }
            }

预期输出

需要填写电子邮件


Codeingiter 3 期望按顺序执行规则。如果一个成功然后下一个执行。在这种情况下,如果我将电子邮件字段留空,它会不必要地执行回调。因此它显示错误消息。它应该显示电子邮件字段是必需的而不是电子邮件不存在。

感谢您的反馈。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    我这样做:

    创建文件:/application/libraries/MY_form_validation.php

    <?php if (!defined('BASEPATH')) {
        exit('No direct script access allowed');
    }
    class MY_Form_validation extends CI_Form_validation
    {
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Your custom validation
         */
        public function is_email_exist($str)
            {
                $CI =& get_instance();
                //  code to check email exist in databse here
                if ($str=='test') {
                    return true;  
                }else { 
                    $this->form_validation->set_message('is_email_exist', 'Email Does not exist');
                    return FALSE;
                }
            }
    }
    
    /* End of file MY_form_validation.php */
    
    /* Location: ./application/libraries/MY_form_validation.php */
    

    在控制器中(移除回调):

    ...
    $this->form_validation->set_rules('email', 'Email', 'required|is_email_exist');
    ...
    

    在控制器删除函数中:callback_is_email_exist.

    但是如果你想检查电子邮件是否是唯一的这样做:

    在控制器中:

    ...
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]',['is_unique'=>'Email exist!']);
    ...
    

    【讨论】:

    • 感谢在 MY_form_validation 中创建回调代码对我有用 :)
    猜你喜欢
    • 1970-01-01
    • 2015-07-04
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2021-08-17
    相关资源
    最近更新 更多