You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter表单提交失败时值不保留且无错误提示求助

问题排查与修复方案

嘿,我帮你找到了两个核心问题,咱们逐个解决:

一、控制器的关键错误:重定向丢失验证状态

当表单验证失败时,你用了redirect('Organisation/createNewuser'),这会直接发起全新的HTTP请求,导致之前的表单验证错误信息、用户输入的临时数据全部丢失——CodeIgniter的set_value()和form_error()都是依赖当前请求的验证上下文工作的,重定向后这个上下文就没了,自然无法保留输入值和显示错误。

修正后的控制器代码片段:

public function createOrgUsers() { 
    // 验证规则部分保持不变
    $this->form_validation->set_rules( 'temp_username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[users.username]', array( 'required' => 'You have not provided %s.', 'is_unique' => 'This %s already exists.' ) ); 
    $this->form_validation->set_rules('temp_firstname', 'First Name', 'required'); 
    $this->form_validation->set_rules('temp_lastname', 'Last Name', 'required'); 
    $this->form_validation->set_rules('temp_password', 'Password', 'required'); 
    $this->form_validation->set_rules('temp_confirm_password', 'Password Confirmation', 'required|matches[temp_password]'); 
    $this->form_validation->set_rules('temp_emailid', 'Email', 'required|valid_email|is_unique[users.email]'); 

    if ($this->form_validation->run() == FALSE) { 
        // 不要重定向,直接加载对应的视图(这里要和你显示表单的视图路径一致)
        // 重新获取orgName数据,确保视图能正常渲染
        $org_id = $this->input->post('organisation_id') ?? '';
        $data['orgName'] = $this->db->where('id', $org_id)->get('organisations')->result(); 
        $this->load->view('Organisation/createNewuser', $data); 
    } else { 
        // 原有成功逻辑保持不变
        $data = array( 
            'username' => $this->input->post('temp_username'), 
            'firstname' => $this->input->post('temp_firstname'), 
            'lastname' => $this->input->post('temp_lastname'), 
            'email' => $this->input->post('temp_emailid'), 
            'password' => password_hash($this->input->post('temp_password'), PASSWORD_DEFAULT), 
            'userlevel' => '3', 
            'organisation_id' => $this->input->post('organisation_id'), 
            'timestamp' => time(), 
            'ip' => $this->input->ip_address(), 
            'status' => 1, 
            'regdate' => time() 
        ); 
        $this->Org_model->orgUsers($data); 
        $this->session->set_flashdata('success', 'User created successfully.'); 
        redirect('Organisation/editOrg'); 
    } 
}

二、视图的两处错误:字段关联错误、缺失错误提示

  1. 用户名输入框的set_value参数错误:你把temp_username的value写成了set_value('temp_firstname'),导致用户名无法保留输入值
  2. 多个字段缺失错误提示:Last Name、Password、Confirm Password、Email这几个字段没有调用form_error(),所以验证失败时不会显示对应错误

修正后的视图代码片段:

<div class="panel-body"> 
    <?php echo validation_errors(); ?> 
    <form action="<?php echo base_url(); ?>Organisation/createOrgUsers" id="user-groups-create" class="form-horizontal" method="post"> 
        <div id="infoMessage" class="error" ><?php echo $this->session->flashdata('message'); ?></div> 

        <div class="form-group"> 
            <label class="col-sm-4 control-label">Username : </label> 
            <div class="col-md-8"> 
                <!-- 修正:set_value的参数改为temp_username -->
                <input type="text" name="temp_username" class="form-control" value="<?php echo set_value('temp_username'); ?>" /> 
            </div> 
            <?php echo form_error('temp_username', '<span class="error">', '</span>'); ?> 
        </div> 

        <div class="form-group"> 
            <label class="col-sm-4 control-label">First Name : </label> 
            <div class="col-md-8"> 
                <input type="text" name="temp_firstname" class="form-control" value="<?php echo set_value('temp_firstname'); ?>" /> 
            </div> 
            <?php echo form_error('temp_firstname', '<span class="error">', '</span>'); ?> 
        </div> 

        <div class="form-group"> 
            <label class="col-sm-4 control-label">Last Name : </label> 
            <div class="col-md-8"> 
                <input type="text" name="temp_lastname" class="form-control" value="<?php echo set_value('temp_lastname'); ?>" /> 
            </div> 
            <!-- 添加:Last Name的错误提示 -->
            <?php echo form_error('temp_lastname', '<span class="error">', '</span>'); ?> 
        </div> 

        <div class="form-group"> 
            <label class="col-sm-4 control-label">Password : </label> 
            <div class="col-md-8"> 
                <input type="text" name="temp_password" class="form-control" value="<?php echo set_value('temp_password'); ?>" /> 
            </div> 
            <!-- 添加:Password的错误提示 -->
            <?php echo form_error('temp_password', '<span class="error">', '</span>'); ?> 
        </div> 

        <div class="form-group"> 
            <label class="col-sm-4 control-label">Confirm Password : </label> 
            <div class="col-md-8"> 
                <!-- 密码确认框可保留空值,也可以用set_value,根据需求选择 -->
                <input type="password" name="temp_confirm_password" class="form-control" value="<?php echo set_value('temp_confirm_password'); ?>" /> 
            </div> 
            <!-- 添加:Confirm Password的错误提示 -->
            <?php echo form_error('temp_confirm_password', '<span class="error">', '</span>'); ?> 
        </div> 

        <div class="form-group"> 
            <label class="col-sm-4 control-label">Email Id : </label> 
            <div class="col-md-8"> 
                <input type="text" name="temp_emailid" class="form-control" value="<?php echo set_value('temp_emailid'); ?>" /> 
            </div> 
            <!-- 添加:Email的错误提示 -->
            <?php echo form_error('temp_emailid', '<span class="error">', '</span>'); ?> 
        </div> 

        <div class="form-group"> 
            <div class="col-md-8 col-sm-offset-4"> 
                <input type="hidden" name="organisation_id" value="<?php echo $orgName[0]['id']; ?>"> 
                <button type="submit" value="submit" class="btn btn-primary form-control" id="submit">Submit</button> 
            </div> 
        </div> 
    </form> 
</div>

额外提示

  • 如果你的createNewuser视图需要orgName数据,在控制器验证失败加载视图时,一定要重新获取并传递这个数据,否则视图会报错
  • 密码类型的输入框一般不建议保留用户输入值,所以如果不需要的话,temp_confirm_password的value可以留空,只添加错误提示即可

内容的提问来源于stack exchange,提问作者suresh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:09:03