CodeIgniter - 表单验证设置为false

时间:2012-04-29 17:44:49

标签: php forms codeigniter validation

我有一些图片上传脚本,我正在使用表单验证。所以这是代码:

$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
    }

如果图片上传出现错误,我想提供虚假表格。我想像一些字段的错误(如果它是空的)那样做,以获取后期变量的数据(对于字段的内容)。

最好的方法是什么?

1 个答案:

答案 0 :(得分:0)

else$this->form_validation->run()的{​​{1}}条件应该类似。

表单验证:

$this->upload->do_upload()

文件上传:

$data['error_message'] = validation_errors();
$this->load->view('form', $data);

“表单”视图是最初加载的视图。它可以有这种div:

$data['error_message'] = $this->upload->display_errors();
$this->load->view('form', $data);