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 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流