我有一些用于图像上传的脚本,我正在将其与表单验证一起使用。所以这是代码:
$this->form_validation->set_rules('name','Name','required|max_length[30]|xss_clean');
$this->form_validation->set_rules('desc','Description','required|xss_clean');
if ($this->form_validation->run() == TRUE)
{
$config = array(
'allowed_types' => 'jpg|jpeg|png|gif',
'upload_path' => path/to/folder,
'max_size' => 3000,
'max_height' => 1024,
'max_width' => 1024,
'remove_spaces' => TRUE
);
$this->load->library('upload', $config);
if ($this->upload->do_upload())
{
//code for insert this data into database
}
else
{
//code for FALSE the form validation and error message with flashdata
}
现在,如果图像上传出现一些错误,我想错误形式。我想这样做,就像某个字段的错误(如果它是空的)一样,以取回帖子变量的数据(对于字段的内容)。
最好的方法是什么?
$this->form_validation->run()
和$this->upload->do_upload()
的else
条件应该相似。
表单验证:
$data['error_message'] = validation_errors();
$this->load->view('form', $data);
文件上传:
$data['error_message'] = $this->upload->display_errors();
$this->load->view('form', $data);
"表单"视图是最初加载的视图。它可以有这样的div:
<div class="error_message">
<?php if ($error_message) {
echo $error_message;
} ?>
</div>