【问题标题】:'matches' form validation rule in codeignitercodeigniter 中的“匹配”表单验证规则
【发布时间】:2014-02-22 09:23:31
【问题描述】:

我只是想知道我们可以在 CodeIgniter 中使用它吗..

$this->form_validation->set_rules('marriage_id', 'Marriage ID','required|is_unique[marriage.marriage_id]|matches[person.marriage_id]');

marriage 是我的表名,marriage_id 是该表中的一个字段。

Person 是另一个表,它还有marriage_id field.From 以上控制器代码,我正在将数据插入marriage 表中,我想知道我可以检查一下,它与marriage_id 匹配是否在人表中。

【问题讨论】:

  • 您可能需要使用callback function 作为自定义验证方法来搜索Person 表以检查当前mariage_id 是否存在。

标签: forms codeigniter validation


【解决方案1】:

根据 Codeigniter 参考,您只能使用“is_unique”使用 table.field,但不能使用“matches”(匹配检查另一个表单元素,而不是 db 表)。

你应该使用自定义回调来检查你需要什么,请阅读http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html#callbacks

【讨论】:

    【解决方案2】:

    不,你不能。您只能将 table_name.feild_name 与 is_unique 一起使用

    但是你可以编写一个回调函数来检查你需要的验证

    像这样的

    $this->form_validation->set_rules('username', 'Username', 'callback_check_match');
    

    以及您的自定义回调函数...

    public function check_match($str)
    {
         $result = $this->model_name->check_databse($str);
         //call to model function fo check string match in databse and return result
         if (return_result) {
              $this->form_validation->set_message('check_match', 'The %s field does not match ur desired feild');
              return FALSE;
         }
         else
         {
             return TRUE;
         }
    }
    

    在您的模型中:

    public function check_databse($str){
     $this->db->select('count(*)',FALSE)->from('table_name')->where('coulmn_name',$str);
     retrun $this->db->get()->result_array();
    }
    

    如需更多参考,请查看以下链接以获取有关 ci 表单验证库的文档

    http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html

    【讨论】:

      猜你喜欢
      • 2015-08-30
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      相关资源
      最近更新 更多