douchensi8625 2016-04-05 12:42
浏览 30
已采纳

表单验证运行false时返回输入值

View

<div class="box-body">
    <div class="form-group">
        <label for="FNAME" class="col-sm-2 control-label col-sm-offset-2">  <span>*</span>First Name:</label>
        <div class="col-sm-5">
            <input type="text" class="form-control" id="FNAME" name="FNAME">
        </div>
    </div>
    <div class="form-group has-error col-sm-offset-7">
        <label class="control-label" for="error"><?php echo form_error("FNAME"); ?></label>
    </div>
</div>

Controller

public function create_id() {

    $this->form_validation->set_rules('FNAME', 'First Name'         ,'trim|required|max_length[30]');
    $this->form_validation->set_rules('MNAME', 'Middle Name'            ,'trim|max_length[30]');
    $this->form_validation->set_rules('SURNAME', 'Last Name'                ,'trim|required|max_length[30]');

    if($this->form_validation->run($this) == FALSE) {
        $this->add_view();
    } else {
        if($query = $this->Employees_Model->insert()) {
            $this->autoid();
            redirect('Employees/index');
        } else {
            $this->add_view();
        }
    }   
}

What I want is if($this->form_validation->run($this) == FALSE){as you see it will redirect back to view. $this->add_view(); all I want is when it redirects back, the data that I input will remain. so that I will not input it again when the validation fails.

  • 写回答

3条回答 默认 最新

  • dqkx69935 2016-04-05 13:08
    关注

    As per CodeIgniter documentation, you need to alter your view file a little bit, in the means of, printing those input element values with <?= set_value("FNAME"); ?> in the input element _value_ parameter. So your

    <input type="text" class="form-control" id="FNAME" name="FNAME">
    

    would become

    <input type="text" class="form-control" id="FNAME" name="FNAME" value="<?= set_value("FNAME"); ?>">
    

    and so on. This way you will tell CodeIgniter to re-populate the form after an error.

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效