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

时间:2017-07-21 04:05:01

标签: codeigniter validation tinymce-4

我正在使用tinymce来添加与应用程序相关的用户求职信。 这就是我的帖子数组的样子:

Array
(
[cover_letter] => <p>Test Cover Letter</p>
    <ol>
    <li>Need to save this data</li>
    </ol>
    <p><strong>Thanks</strong></p>
)

我只是使用了require验证规则。

'candidate_cover_letter' => array(
    array(
            'field' => 'cover_letter',
            'label' => 'Cover Letter',
            'rules' => 'required'
    )
)

我收到有关此问题的验证错误,例如求职信要求

我有两个主要问题:

  1. 如何验证HTML发布数组数据
  2. 这是保存此类数据的最佳做法吗?如果没有那么我应该如何保存这些数据?

2 个答案:

答案 0 :(得分:0)

首先,在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);

您可以参考here

所以,你的代码在控制器中应该是这样的:

$config =array(
    array(
            'field' => 'cover_letter',
            'label' => 'Cover Letter',
            'rules' => 'required'
    )
);
$this->form_validation->set_rules($config);

您可以在$ config中添加额外字段,如上例所示。

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

我建议您在数据库表中使用“TEXT”类型的字段,它应该没问题。

答案 1 :(得分:0)

点击提交后,您将被重定向回某个控制器。使用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
}

设置表单validation.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'
      ],

   ],
]