表格验证&重定向错误后重新填充字段值;验证工作,但价值观没有(CodeIgniter 3)

时间:2015-12-10 10:30:42

标签: php codeigniter codeigniter-3

我试图在重定向验证错误后保留字段值。验证错误显示正常但我不断丢失字段值

形式:

<form>
<input name="v_item_title" placeholder="Property Title Goes Here.." value="<?php echo set_value('v_item_title'); ?>" />
<input type="submit" value="Submit">
</form>

控制器:

$this->load->helper('security');
$this->load->library('form_validation');
$this->form_validation->set_rules('v_item_title', 'Property title', 'trim|required|xss_clean|max_length[100]');
if($this->form_validation->run() == FALSE)
{
$this->session->set_userdata('validation_errors', validation_errors());
$this->session->mark_as_flash('validation_errors'); // data will automatically delete themselves after redirect
$this->session->set_flashdata('v_item_title', $this->input->post());
$this->session->flashdata('v_item_title');
redirect('user/dashboard#new');
} else {

重定向到

public function dashboard()
{
if($this->session->userdata('is_logged_in')){
$data['validation_errors'] = $this->session->userdata('validation_errors');
$data['v_item_title'] = $this->session->userdata('v_item_title');
$data['homepage'] = '../../templates/vacations/users/dashboard';
$this->load->view('template_users',$data);
}else{

3 个答案:

答案 0 :(得分:3)

您在控制器上的表单验证错误,您在虚假区域有成功信息,它应该在真正的区域,如

http://www.codeigniter.com/user_guide/libraries/form_validation.html

不确定您的控制器名称是什么,因此我将示例命名为login

应用程序&gt;控制器&gt; Login.php

<?php

class Login extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('security');
        $this->load->library('session');
        $this->load->library('form_validation');
    }

    public function index() {

    $data['title'] = 'Login';

    $this->form_validation->set_rules('name_check_box', '', 'trim|required|callback_checkbox');
    $this->form_validation->set_rules('v_item_title', 'Property title', 'trim|required|xss_clean|max_length[100]');

    if($this->form_validation->run() == FALSE) {

        // Load the view
        $this->load->view('header', $data);
        $this->load->view('login', $data);
        $this->load->view('footer');


    } else {

        $data = array(
           'is_logged_in' => true,
           'validation_errors' => validation_errors(),
           'v_item_title' => $this->input->post('v_item_title')
        );

        $this->session->set_userdata($data);

        // data will automatically delete themselves after redirect

        $this->session->mark_as_flash('validation_errors'); 

        // You could set the title in session like above for example
        $this->session->set_flashdata('v_item_title', $this->input->post('v_item_title'));

        // Echo flash data on view file?
        // $this->session->flashdata('v_item_title');

        // Dashboard will be a separate controller
        // application > controllers > user > Dashboard.php
        redirect('user/dashboard');

    }

    public function checkbox() {

        if (isset($_POST['name_check_box']) {
            return true;
        } else {
            $this->form_validation->set_message('checkbox', 'Check box needs to be checked');
            return false;
        }
    }

}

查看

http://www.codeigniter.com/user_guide/helpers/form_helper.html

<?php echo form_open('login');?>

<input name="v_item_title" placeholder="Property Title Goes Here.." value="<?php echo set_value('v_item_title'); ?>" />

<input type="checkbox" name="name_check_box"> Something <br>

<input type="submit" value="Submit"> 

<?php echo form_close();?>
  

注意:您可能需要在应用程序中设置自定义路由&gt; config&gt;   routes.php文件

     

http://www.codeigniter.com/user_guide/general/routing.html

答案 1 :(得分:1)

试试这个

查看

<form>
<input name="v_item_title" placeholder="Property Title Goes Here.." value="<?php echo $this->session->flashdata('v_item_title'); ?>" />
<input type="submit" value="Submit">
</form>

确保加载会话库并形成帮助

答案 2 :(得分:0)

我有同样的问题。我为我应用了这种逻辑及其工作 像这样更改您的添加和编辑方法...

public function add(){
        $college = $this->session->flashdata('data');
        $this->load->view("college_add", compact('college'));
    }
    public function edit(){
        $college_id = $this->uri->segment(3);
        if($college_id)
        {
            $college = $this->session->flashdata('data');
            if(empty($college))
            $college = $this->college->get_college_details_secure($college_id);             
            $this->load->view('college_add', compact('college'));
        }
        else
            redirect('college/add');
    }

然后您的save方法将这样重定向。

if ($this->form_validation->run() != TRUE)
{               
    $this->set_flashdata("message","Ooopps... form validation failed.".validation_errors());
    $this->session->set_flashdata('data', $this->input->post());
    if($college_id == '')
        redirect("college/add");
    else
        redirect("college/edit/$college_id");                       
}