【问题标题】:Form Validation & Re-Populating Field Values After Redirect Error; Validation Works, But Values Doesn't (CodeIgniter 3)重定向错误后表单验证和重新填充字段值;验证有效,但值无效 (CodeIgniter 3)
【发布时间】:2016-03-15 22:43:32
【问题描述】:

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

表格:

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

【问题讨论】:

    标签: php codeigniter codeigniter-3


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      不确定你的控制器名称是什么所以我命名为示例登录

      应用程序 > 控制器 > 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();?>
      

      注意:您可能需要在 application > config > 中设置自定义路由 路由.php

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

      【讨论】:

      • 感谢 wolfgang1983 的提示,除了自定义路由外,按照您的建议进行操作。同样根据 monace19,我添加了 session->flashdata('v_item_title'); ?> 到表格。但是在将 v_item_title 放入 $this->session->set_flashdata('v_item_title', $this->input->post('MISSED')); 之后它实际上起作用了当我发布问题时我错过了。
      • 还有一件事,你知道在这种情况下如何对选择和复选框做同样的事情吗?
      • 当我达到 15 个声望时会显示投票。顺便说一句,我已经整理好了,stackoverflow.com/questions/34207957/…
      【解决方案3】:

      试试这个

      查看

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

      确保加载会话库和表单助手

      【讨论】:

      • 嗨 Monace19,在添加 'session->flashdata('v_item_title'); 之后?>' 我在字段中得到 Value=Array。有什么建议吗?
      • 谢谢,我把 v_item_title 放到 $this->session->set_flashdata('v_item_title', $this->input->post('MISSED'));当我发布问题时我错过了。
      猜你喜欢
      • 2010-11-10
      • 2011-10-29
      • 2013-01-06
      • 2013-03-24
      • 2011-09-13
      • 2012-05-13
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多