dqwd71332 2014-03-31 19:04
浏览 52
已采纳

CodeIgniter form_error返回错误消息,set_value在第一页加载时不返回任何内容

When using CodeIgniter's Form Validation class, the form_errors in the view always show the error messages. Also, using the set_value method never populates the field with the correct values unless there's no validation against it.

I have extended the form_validation class, but only to add a new method that I needed and removing the code entirely does not make a difference.

The code for my controller is:

function test() {
    $data['title'] = 'My title';
    $this->form_validation->set_rules('title', 'Title', 'trim|required');
    if($this->form_validation->run() == FALSE) {
        $this->load->view('form', $data);
    } else {
        $this->load->view('form', $data);
    }
}

The code for my view is:

echo form_open(current_url()).
     form_error('title').
     form_label('Title', 'title').
     form_input(array('name' => 'title', 'id' => 'title'), set_value('title', $title)).
     form_label('Other', 'other').
     form_input(array('name' => 'other', 'id' => 'other'), set_value('other', 'test')).
     form_submit('submit', 'Update').
     form_close();

What I expect is that there are no error messages show, the first input field would have the text 'My title' and the second field would have the text 'other'. If I submitted the forms, then they would show the posted values.

In reality, I always get an error message saying that the first field is required, the first field is always empty when the page loads and the second field is filled correctly with the text 'other'.

If I fill in both/either of the fields and post the form, the fields are returned with the post data, which is correct. I cannot see what I am doing wrong and any help is appreciated.

  • 写回答

2条回答 默认 最新

  • drwjv28028 2014-04-04 19:18
    关注

    Double check your code in the file

    /system/libraries/form_validation.php

    line 77 should be

    if (count($_POST) == 0)
    

    instead of

    if (!isset($_POST))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退