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 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算