由于重定向而未显示CodeIgniter表单验证

时间:2017-01-19 14:07:12

标签: php codeigniter

我有这个编辑页面,它从uri段获取其id和状态。问题是,如果用户没有选择图像或完成表单的一部分,则应该重新加载页面并显示表单的验证错误。但是,该页面重新加载会导致表单中的数据丢失。有没有办法解决这个问题?任何帮助将不胜感激。我附上了我的观点和控制器

查看

<?php if($edit == "false"){
        echo form_open_multipart('Control/Products/ProductDetail/addProduct','class="productdetail"');
 }else{
        echo form_open_multipart('Control/Products/ProductDetail/editProduct','class="productdetail"');
       }?>
                            <label for="inputproductname">Product Name</label>
                            <input type="text" class="form-control" id="inputproductname" name="inputproductname" placeholder="Name" value="<?php echo $name; ?>">
                            <label for="inputproductdescription">Product Description</label>
                            <textarea class="form-control" id="inputproductdescription" name="inputproductdescription" placeholder="Description" rows="7" 
                            ><?php echo $description; ?></textarea>
                            <label for="inputproductprice">Product Price</label>
                            <input type="price" class="form-control" id="inputproductprice" name="inputproductprice" placeholder="Price" value="<?php echo $price; ?>">
                            <label for="inputproductimage">Product Image</label>
                            <p><input type="file" class="form-control-file" name="upload" id="upload" aria-describedby="fileHelp"></p>
                            <input type="hidden" class="form-control" id="inputcurrentid" name="inputcurrentid" value="<?php echo $currentid; ?>">
                            <input type="hidden" class="form-control" id="inputcurrentstatus" name="inputcurrentstatus" value="<?php echo $currentstatus; ?>">
            <button type="submit" class="btn btn-primary">
                     <?php if($edit == "false"){
                         echo "Add";
                      }else{
                          echo "Edit";
                      }?>
             </button>
<a href="<?php echo base_url();?>Control/Products/Products">Cancel</a>
<?php echo form_close(); ?>
<?php echo validation_errors(); ?>
<p><?php echo $this->session->flashdata('Form'); ?></p> 

控制器

public function index(){
      $productid = $this->uri->segment(5);
      $editstatus = $this->uri->segment(6);
      if($editstatus == "false"){
          $data['name'] = '';
          $data['description'] = '';
          $data['price'] = '';
          $data['edit'] = "false";
          $data['message']='';
          $data['currentid'] = '';
          $data['currentstatus'] = '';
      }else{
          $product = $this->ProductsModel->getProduct($productid);
          foreach ($product as $productdetail){
            $data['name'] = $productdetail->name;
            $data['description'] = $productdetail->description;
            $data['price'] = $productdetail->price;
          }
          $data['edit'] = "true";
          $data['message']='';
          $data['currentid'] = $productid;
          $data['currentstatus'] = $editstatus;
      }
      $this->load->view('control/controlMenu/navigationLink');
      $this->load->view('control/controlProducts/productDetail',$data);
      $this->load->view('control/controlMenu/navigationJquery');
    }

public function editProduct(){
      $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
      $this->form_validation->set_rules('inputproductname', 'Name', 'trim|required');
      $this->form_validation->set_rules('inputproductdescription', 'Description', 'trim|required');
      $this->form_validation->set_rules('inputproductprice', 'Price', 'trim|required');
      if (empty($_FILES['userfile']['name']))
      {
          $this->form_validation->set_rules('upload', 'Image', 'required');
      }

      $inputproductname = $this->input->post('inputproductname');
      $inputproductdescription = $this->input->post('inputproductdescription');
      $inputproductprice = $this->input->post('inputproductprice');
      $inputdateadded = date('Y-m-d');
      $inputcurrentid = $this->input->post('inputcurrentid');
      $inputcurrentstatus = $this->input->post('inputcurrentstatus');

      $config['upload_path'] = $this->getProductImageFolderPath();
      $config['allowed_types'] = 'jpg|jpeg|png'; 
      $config['max_size'] = 3000;  
      $config['remove_spaces'] = TRUE;
      $config['overwrite'] = TRUE;
      $config['file_name'] = $inputproductname;
      $this->load->library('upload', $config);


      if($this->form_validation->run()==false){
          redirect('/Control/Products/ProductDetail/index/'.$inputcurrentid.'/'.$inputcurrentstatus);
        }else{
          if(!$this->upload->do_upload('upload')){
            $this->session->set_flashdata('Form',$this->upload->display_errors());
            redirect('Control/'.$this->getCurrentModule().'/'.$this->getClassName());
          }else{
              $extension = $this->upload->data('file_ext');
              $productdetails = array(
                'name'=>$inputproductname,
                'description'=>$inputproductdescription,
                'price'=>$inputproductprice,
                'imagePath'=>$config['upload_path'].$config['file_name'].$extension,
                'dateAdded'=>$inputdateadded
              );
              $this->db->trans_start();
              $this->ProductsModel->editProduct($productid,$productdetails);
              $this->db->trans_complete();
              if($this->db->trans_status()===false){

              }else{
                  $this->session->set_flashdata('Form', $inputproductname . ' has been altered on the database');
                  redirect('/Control/Products/Products');
              }
          }
        }
  }

2 个答案:

答案 0 :(得分:0)

if($this->form_validation->run()==false){ redirect('/Control/Products/ProductDetail/index/'.$inputcurrentid.'/'.$inputcurrentstatus); 我认为这个重定向stmt的问题不使用它,而是你需要使用加载视图,以便它将保留错误数组。

答案 1 :(得分:0)

只要在您的控制器中做

 if ($this->form_validation->run() == FALSE) {
        $this->session->set_flashdata('field', form_error('field', '<span class="text-danger pl-3">', '</span>'));

        redirect("url");
    } else {
        # code...

    }

并在您的重定向页面中使用

<?= $this->session->flashdata('field'); ?>