doucang2831 2012-02-04 12:52
浏览 59
已采纳

set_value不起作用

I have the form_validation library loaded in my controller and the form validation itself is working, but when using set_value() it's not populating the form fields. Here is the code in to controller:

function addUser()
{
    $this->form_validation->set_rules('firstName', 'firstname', 'trim|required|max_length[30]');
    $this->form_validation->set_rules('surname', 'surname', 'trim|required|max_length[30]');
    $this->form_validation->set_rules('emailAddress', 'email address', 'trim|required|valid_email|is_unique[users.email]|max_length[255]');
    $this->form_validation->set_rules('password', 'password', 'trim|required|max_length[20]|min_length[5]');
    $this->form_validation->set_rules('passwordVerify', 'password verification', 'trim|required|max_length[20]|min_length[5]');

    if($this->form_validation->run() === FALSE) {
        $this->session->set_flashdata('formValidationError',  validation_errors('<p class="error">', '</p>'));
        redirect('/member/register');
    } else {
        echo 'Passed';
    }
}

And here is the code in the view:

    <?php echo $this->session->flashdata('formValidationError'); ?>
<form method="POST" action="<?php echo site_url('member/addUser'); ?>">
    <fieldset>
        <legend>Create a FREE account</legend>
        <div>
            <label for="firstName">Firstname</label>
            <input type="text" name="firstName" value="<?php echo set_value('firstName'); ?>" maxlength="30">
        </div>

        <div>
            <label for="surname">Surname</label>
            <input type="text" name="surname" value="<?php echo set_value('surname'); ?>" maxlength="30">
        </div>

        <div>
            <label for="emailAddress">Email Address</label>
            <input type="text" name="emailAddress" value="<?php echo set_value('emailAddress'); ?>" maxlength="255">
        </div>

        <div>
            <label for="password">Password</label>
            <input type="password" name="password" maxlength="20">
        </div>

        <div>
            <label for="passwordVerify">Verify Password</label>
            <input type="password" name="passwordVerify" maxlength="20">
        </div>

        <button type="submit">Register</button>
    </fieldset>
</form>

Is there something I am missing? Is the redirect causing the issue?

  • 写回答

2条回答 默认 最新

  • douhan8430 2012-02-04 13:03
    关注

    It is not working because you are using redirection.

    Instead of

        $this->session->set_flashdata('formValidationError',  validation_errors('<p class="error">', '</p>'));
        redirect('/member/register');
    

    just for testing, try to load the view

        $this->load->view('member_register_view');
    

    and you will see.

    set_value requires that the form validation ran in the same context... you lose this context when you redirect.

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料