dongxun6458 2017-01-13 14:19
浏览 53
已采纳

对外部库中的表单验证规则使用回调函数 - Codeigniter

I am learning codeigniter and I have two questions about form validation. I have a controller, that uses an array of configuration for the form validation. Something like this:

public $validacao = array(
        array(
                'field' => 'nome',
                'label' => 'Nome',
                'rules' => 'required',
                "errors" => array('required' => " Informe o %s. ")
        ),
        array(
                'field' => 'descricao',
                'label' => 'Descrição',
                'rules' => 'required',
                "errors" => array('required' => " Informe a %s. ")
        ));

I have a custom library, named IncFuncoes.php, inside application/libraries. It only has a function that validates date. I wanna call this function as a callback on the 'rules' of my array, but it doesnt work. After I loaded it inside the constructor, I did something like this:

public $validacao = array(
            array(
                    'field' => 'nome',
                    'label' => 'Nome',
                    'rules' => 'required',
                    "errors" => array('required' => " Informe o %s. ")
            ),
            array(
                    'field' => 'descricao',
                    'label' => 'Descrição',
                    'rules' => 'required',
                    "errors" => array('required' => " Informe a %s. ")
            )
            array(
                    'field' => 'data',
                    'label' => 'Data',
                    'rules' => 'required|$this->incfuncoes->validatedate',
                    "errors" => array('required' => " Informe a %s. ")
            ));

But it didnt work. If I create a function inside my controller that only calls the function inside my custom library, it works. But I was wondering if there is any way that I could call the function inside my library directly on the 'rules' of my configuration array.

My other question is, how do I customize an error message using this configuration array for my form validation. Cause the way that I am doing is for each validation, like required for instance, I create an error message. I wanna create one error for each input. Like, if the field is empty or the date format is not right, I only need the message "Wrong date format.". I know that could use something like this:

$this->form_validation->set_message('name', 'required', 'Enter your Name');

But I wanna use only my configuration array to set everything. Is it possible?

  • 写回答

1条回答 默认 最新

  • doumouyi4039 2017-01-13 16:02
    关注

    What you should do is extend the form validation library itself.

    class MY_Form_validation extends CI_Form_validation
    {
        public function validatedate($input)
        {
            // set error message
            // do your validation
            // make sure to return true or false
        }
    }
    

    Then apply via:

    'rules' => 'required|validatedate',
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算