重定向错误后的表单验证和重新填充字段值;验证有效,但值无效 (CodeIgniter 3)
Form Validation & Re-Populating Field Values After Redirect Error; Validation Works, But Values Doesn't (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{
试试这个
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>
确保加载会话库和表单助手
你在控制器上的表单验证错误你在错误区域有你的成功信息它应该在真实区域像
http://www.codeigniter.com/user_guide/libraries/form_validation.html
不确定您的控制器名称是什么,所以我命名为 example login
应用程序 > 控制器 > 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();?>
Note: you may need to set custom routes in application > config >
routes.php
我有同样的问题。我应用了这个逻辑及其对我的工作
像这样更改您的添加和编辑方法...
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');
}
并且您的保存方法像这样重定向..
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");
}
我试图在重定向验证错误后保留字段值。验证错误显示正常,但我不断丢失字段值
表格:
<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{
试试这个
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>
确保加载会话库和表单助手
你在控制器上的表单验证错误你在错误区域有你的成功信息它应该在真实区域像
http://www.codeigniter.com/user_guide/libraries/form_validation.html
不确定您的控制器名称是什么,所以我命名为 example login
应用程序 > 控制器 > 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();?>
Note: you may need to set custom routes in application > config > routes.php
我有同样的问题。我应用了这个逻辑及其对我的工作 像这样更改您的添加和编辑方法...
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');
}
并且您的保存方法像这样重定向..
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");
}