简体   繁体   中英

Form Validation & Re-Populating Field Values After Redirect Error; Validation Works, But Values Doesn't (CodeIgniter 3)

I am trying to keep field values after validation errors on redirect. Validation error shows fine but I keep loosing field values

Form:

<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>

Controller:

$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 {

Redirects to

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{

You have form validation wrong on controller you have your success info on false area it should be in true area like

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

Not sure what your controller name is so I named example login

application > controllers > 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;
        }
    }

}

View

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();?>

Note: you may need to set custom routes in application > config > routes.php

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

Try this

VIEW

<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>

Make sure that you load the session library and form helper

i have same issue. i applied this logic and its works for me Change your add and edit method like this...

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');
    }

And Your save method redirect like this..

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");                       
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM