在数组输入上设置验证规则

时间:2014-03-11 03:40:03

标签: arrays codeigniter validation if-statement error-handling

<input type='text' name='names[]'/>


$validation = array(array('field' => names, 'label'=> , 'rules'=>'required'););

问题。

我如何能够在名称1中验证数组值?

我非常感到困惑,因为我正在努力追回自己如何做到这一点。

'field' => 'names[$i]' - 我确定这是错误的。

我正在为我的数据库添加食谱。简单添加CRUD功能。但我希望它是一个聪明的人。所以我提出了很多ifs来捕获任何错误,以便能够记录它们并向用户显示原因。

function recipe_add(){
    if(array_key_exists('submit',$_POST)){
    $this->form_validation->set_rules('recipename','Recipe Name', 'trim|required');
    $this->form_validation->set_rules('recipedescription','Recipe Description', 'trim|required');
    $this->form_validation->set_rules('recipeinstruction','Recipe Instruction','trim|required');
    if($this->form_validation->run()==FALSE){$this->load->view('Admin/insert_recipe2');}else{
        $ingredients = array();
        for($i=0;$i<10;$i++){
        if($_POST['ingredient'][$i]!=null AND ! preg_match('#[0-9]#',$_POST['ingredient'][$i]) AND is_numeric($_POST['quantity'][$i])){
        $ingredients[$_POST['ingredient'][$i]] = $_POST['quantity'][$i].'-'.$_POST['measurement'][$i];}
        } if(count($ingredients)<1){$this->load->view('Admin/insert_recipe2');}
        else{
        $config['upload_path'] = './Admin_uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000';
        $config['max_width']  = '270';
        $config['max_height']  = '280';     
        $this->load->library('upload', $config);        
        if (!$this->upload->do_upload('recipeimage')){
        $this->load->view('Admin/insert_recipe2');
        }else{
        $recipetoInsert['image'] = $this->upload->data();
        $recipetoInsert['name'] = $_POST['recipename'];
        $recipetoInsert['description'] = $_POST['recipedescription'];
        $recipetoInsert['instruction'] = $_POST['recipeinstruction'];
        print_r($recipetoInsert);
        /**if(!$this->KGAdminCRUDModel->insert_recipe($recipetoInsert)){
        $data['Insertion_Error'] = 'Adding recipe to Database Failed.';
        $data['Insertion_Error_Reason'] = '';
        $this->load->view('Admin/insert_recipe2',$data);}
        else{
        $data['Success_Message'] = 'Recipe Added successfully to the Database';
        $data['Success_Data_Details'] = $recipetoInsert;
        $this->load->view('Admin/insert_recipe2',$data);}**/
        }
        }
        }
        else{ $this->load->view('Admin/insert_recipe2');}}

我遇到了ifs纠结的麻烦。我有意外的错误。

0 个答案:

没有答案