如何在自定义回调()中传递3个参数 - codeigniter

时间:2015-04-17 11:06:46

标签: codeigniter callback

配置/ login_rules.php

这里是回调函数 check_email_existence

我想传递三个参数(电子邮件,表格,字段)

<?php


/**
* SETTING VALIDATION RULES FOR THE LOGIN FORM
*/

$config['login_settings'] = array(

        array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required|trim|min_length[6]|max_length[20]|xss_clean',
                'errors' => array(
                        'required' => 'You must provide a %s.',
                ),
        ),

        array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required|trim|valid_email|xss_clean|check_email_existence'
        )
);
?>

我扩展了表单验证助手 libraries / MY_Form_validation.php

<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class MY_Form_validation extends CI_Form_validation {

        protected $CI;

        public function __construct($config = array())
        {
                parent::__construct($config);

                $this->CI =& get_instance();
        }



        function check_email_existence($email,$table,$field) {   

            $this->CI->form_validation->set_message('check_email_existence', 'This %s id is not registered.');
            $query = $this->CI->db->query("SELECT COUNT(*) AS count FROM $table WHERE $field = '".$email."' ");
            $row = $query->row();
            return ($row->count > 0) ? 'success' : 'failure';

        }


 }//class

?>

1 个答案:

答案 0 :(得分:0)

回调函数接受两个参数:($ postdata,$ param)。

$ param在规则名称之后通过[...]接收你定义为规则参数的任何内容:

'rules' => 'required|trim|valid_email|xss_clean|check_email_existence[table,field]'

function check_email_existence($email, $param) {
  // extract table and field from $param
...