我使用丁姆齐来添加与应用程序相关的用户求职信。这是我的post数组的样子:
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'
)
)我得到了与求职信要求类似的验证错误。
我有两个主要问题:
HTML post数组数据发布于 2017-07-21 06:22:42
首先,在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“的字段,这对您来说应该是可以的。
发布于 2017-07-21 09:56:41
点击submit之后,就会被重定向到控制器的某个位置。利用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'
],
],
]https://stackoverflow.com/questions/45228765
复制相似问题