donglian6625 2010-11-17 07:06
浏览 42
已采纳

如何验证Codeigniter中结合的多个字段?

Is there a creative and easy way to check many form fields at once.

I have a form with generated fields on the fly, each has a unique id.

The thing is submitting all the fields are not required, but at least one field must be filled before submitting.

Is there a way to do this in Codeigniter, or how would I go about validating this effectively.

I understand that it is possible to check each field individually, but I'm seeking for a much cleaner way.

I hope it's clear for you guys. Thanks.

  • 写回答

3条回答 默认 最新

  • doujindou4356 2010-12-06 15:26
    关注

    Right now, I have a custom function which will batch check every submission on my form to make sure it meets a set of rules. I am thinking that it may work to set up my form as an html array (data[]), retrieve it in code igniter, then use the CI callback_ validation function to make sure everything turns out okay. It seems complex, so I haven't yet entirely wrapped my head around it, but maybe this can get your wheels turning in the right direction.

    EDIT:

    $this->load->library('form_validation');
    
    // If there is any posted data, then we should assign it to our $post_data array.
    
    $post_data = $this->input->post('project_data');
    if (empty($post_data)) {die('empty form');}
    
    // Now, we are ready to validate the incoming data.
    // We will send the data through a callback function which will check to make sure it is valid.
    // If it is not valid, the callback function will trigger a codeigniter validation error.
    
    // Let's temporarily remove any commas from the submission data to avoid delimiter confusion when sending it through the callback
    
    $post_data = str_replace(",", "DELIMITEDCOMMA", $post_data);
    
    $post_data_str = http_build_query($post_data);
    $this->form_validation->set_rules("project_data[errors]", 'Errors', "required|callback__validate_project_data[$post_data_str]");
    
    $this->form_validation->run();
    

    Then, just write your custom validation function based on what it is that you need to validate.

    function _validate_project_data($value, $request)
    {
        // A callback rule check is being attempted by the CI validator 
        // $value is the actual value of the submission, while $request is the key and value
    
        $request = explode(",", $request);
        $request = str_replace("DELIMITEDCOMMA", ",", $request);
    
        // rename the keys in the request back to the original convention
    
        parse_str($request[0], $request); 
        //var_dump($request);
    
        // perform validation here and return true or false (valid or invalid)      
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)