doukui9491 2017-02-07 12:26
浏览 49
已采纳

codeigniter3表单验证回调函数调用不必要

I am using codeigniter 3.I have a email field in form and using required|callback_is_email_exist rules on email.When I leave email field blank,it shows callback message instead of required message. I had worked on codeigniter 2 ,it perefctly shows "required" message, CI3 form validation not executing rules in the sequence when callbacks are used. Following is my code


View : 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>

Controller: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;
                    }
            }

Expected Output

email filed is required


Codeingiter 3 expects to execute rule in sequence.If one succeeds then next one executes.In this case if I leave email field blank it executes callback unnecessarily.Thus it shows wrong message.It should display email field is required instead of email does not exist.

Appreciate your feedback.

  • 写回答

1条回答 默认 最新

  • doubingling4706 2017-02-07 14:43
    关注

    I do it so:

    Create file: /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 */
    

    In controller (remove callback):

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

    In controller remove function: callback_is_email_exist.

    But If you want to check if the email is unique to do so:

    In controller:

    ...
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]',['is_unique'=>'Email exist!']);
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?