dongwei2983 2017-01-19 14:07
浏览 53

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

I have this edit page that has gets its id and status from uri segments. The problem is that if the user doesn't select an image or complete a part of the form, the page is supposed to reload and show the validation errors for the form. However that page reload causes the data in the form to go missing. Is there a way to solve this issue? Any help would be greatly appreciated. I attached my view, and controller

View

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

Controller

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条回答 默认 最新

  • duanjiao2978 2017-01-19 14:12
    关注

    if($this->form_validation->run()==false){ redirect('/Control/Products/ProductDetail/index/'.$inputcurrentid.'/'.$inputcurrentstatus); I think the problem with this redirect stmt don't use it instead you need to use load view so that it will preserves the errors array.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度