Welcome Guest, Not a member yet? Register   Sign In
form validation with file uploads
#1

[eluser]mpar612[/eluser]
Hi Everyone,

I am new to Codeigniter and the forums. I have a form where a user will enter some data and select a file to upload. I understand the form validation and file upload classes have their own errors, but how do you make the two work together? If a user does not complete all of the form fields properly, and does not select a file or the file is of the wrong type, how do you display the errors for both at the same time?

My code is posted below.

Any help would be greatly appreciated. Thanks in advance!

Code:
<?php
class Admin extends CI_Controller {

public function __construct()
{
  parent::__construct();
  $this->load->model('admin_model');
  $this->load->library('cart');
  $this->load->helper('form','url');
  $this->load->library('form_validation');
}

public function index()
{
  $this->load->view('admin/create', array('error' => ' ' ));
}

public function create()
{
  $data['title'] = 'Create a shop item';
  
  $this->form_validation->set_rules('name', 'Name', 'required');
  $this->form_validation->set_rules('price', 'price', 'required');
  $this->form_validation->set_rules('description', 'description', 'required');
  
  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '100000';
  $config['max_width']  = '102400';
  $config['max_height']  = '76800';

  $this->load->library('upload', $config);
  
  if ($this->form_validation->run() === FALSE)
  {
   $this->load->view('templates/header', $data);
   $this->load->view('admin/create');
   $this->load->view('templates/footer');
  }
  else
  {
   $data = array('upload_data' => $this->upload->data());
   $this->upload->do_upload();
   $this->admin_model->set_shop();
  
   $this->load->view('admin/success', $data);
  }
}
}
#2

[eluser]CroNiX[/eluser]
I usually validate file uploads by using a validation callback function. You can create your own error message(s) in the callback function, depending on what you're checking.




Theme © iAndrew 2016 - Forum software by © MyBB