如何在codeigniter中验证post数组中的HTML响应

如何在codeigniter中验证post数组中的HTML响应,codeigniter,validation,tinymce-4,Codeigniter,Validation,Tinymce 4,我正在使用添加与应用程序相关的用户求职信。 这是我的post阵列的外观: Array ( [cover_letter] => <p>Test Cover Letter</p> <ol> <li>Need to save this data</li> </ol> <p><strong>Thanks</strong></p> ) 我得到了与此

我正在使用添加与应用程序相关的用户求职信。 这是我的post阵列的外观:

Array
(
[cover_letter] => <p>Test Cover Letter</p>
    <ol>
    <li>Need to save this data</li>
    </ol>
    <p><strong>Thanks</strong></p>
)
我得到了与此相关的验证错误,如求职信要求

我有两个主要问题:

  • 如何验证
    HTML
    post数组数据
  • 这是这样保存数据的最佳做法吗?如果否,那么我应该如何保存此数据
    首先,在Codeigniter中,如果我们想进行表单验证,我们需要这样做:

        $config = array(
        array(
                'field' => 'username',
                'label' => 'Username',
                'rules' => 'required'
        ),
        array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required',
                'errors' => array(
                        'required' => 'You must provide a %s.',
                ),
        ) 
    );
    
    $this->form_validation->set_rules($config);
    
    你可以参考

    因此,您在控制器中的代码应该如下所示:

    $config =array(
        array(
                'field' => 'cover_letter',
                'label' => 'Cover Letter',
                'rules' => 'required'
        )
    );
    $this->form_validation->set_rules($config);
    
    您可以像上面的例子一样在$config中添加额外的字段

    你问的另一件事是,“你应该如何保存数据?”


    我建议您在数据库表中使用一个类型为“TEXT”的字段,这对您来说应该没问题。

    单击“提交”后,您会被重定向回控制器的某个位置。利用CI表单验证的一种方法是:

    //look to see if you have post data
    if($this->input->post('submit')){
    
        //points to applications/config/form_validation.php (look at next chucnk to set form_validation.php)
        if($this->_validate('cover_letter')){
          //rest of your post logic
    
         //get data to upload to database
         $data = [
             'cover_letter'=>$this->input->post('cover_letter'),
             'user_id'=>$this->input->post('user_id')
    
         ];
           //save data to database ..also this should be moved to a model
           $this->db->insert('name of table to insert into', $data);
        }
        //if you post doesnt not get validated you will fall here and if you have this chucnk of code in the same place were you have the logic to set up the cover letter you will see a pink message that says what ever error message you set up
    }
    
    设置formvalidation.php

    <?php
    
    $config = [
    
        //set up cover letter validation
        //$this->_validate('cover_letter') maps to here and checks this array
        'cover_letter' => [
           [
            'field'=>'cover_letter',
            'label'=>'Cover Letter Required',//error message to return back to user
            'rules'=>'required'//field is required
          ],
          //example to add additional fields to check
          [
            'field'=>'observations',
            'label'=>'Observations',
            'rules'=>'required'
          ],
    
       ],
    ]
    

    我已经解决了这个问题,只是我没有在问题中发布。那么,它对你有用吗?如果有任何错误,请将错误粘贴到这里。我想你没有得到我没有验证HTML输入的点。这就是问题所在。你建议的答案是我已经做过的。
    
    <?php
    
    $config = [
    
        //set up cover letter validation
        //$this->_validate('cover_letter') maps to here and checks this array
        'cover_letter' => [
           [
            'field'=>'cover_letter',
            'label'=>'Cover Letter Required',//error message to return back to user
            'rules'=>'required'//field is required
          ],
          //example to add additional fields to check
          [
            'field'=>'observations',
            'label'=>'Observations',
            'rules'=>'required'
          ],
    
       ],
    ]