Welcome Guest, Not a member yet? Register   Sign In
Validation file input and text input
#2

[eluser]LuckyFella73[/eluser]
If you want to validate POST data and uploaded file you
use callbacks. Your code code look like that:
Code:
function create() //create new post
{          
$this->form_validation->set_rules('content', 'Entry', 'trim|required|xss_clean');
$this->form_validation->set_rules('category_id', 'Category', 'trim|required|xss_clean|integer');
$this->form_validation->set_rules('userfile', 'File', 'callback__do_upload');

//Text input fields
if ($this->form_validation->run() == FALSE)
{
  $this->load->view('new_post');
}      
else
{
  //Add to database
  $data = array (
  'user_id' => $this->tank_auth->get_user_id(),
  'category_id' => $this->input->post('category_id'),
  'content' => $this->input->post('content')
  // maybe save filename into DB too ?
);

$this->Posts_model->create_post($data);

$this->session->set_flashdata('success', 'Post_added!');
redirect('posts');
}      

}



// Form Validation - Callback function
function _do_upload()
{
$config['upload_path'] = './uploads/posts/';
$config['allowed_types'] = 'jpg|png';              
$config['max_size'] = '800'; //in KB

$this->load->library('upload', $config);

//File Upload
if (! $this->upload->do_upload())
{
  $this->form_validation->set_message('_do_upload', $this->upload->display_errors());
  return FALSE;
}
else
{
  return TRUE;
}
}

It's just a rough example but should show you how you do it basically.


Messages In This Thread
Validation file input and text input - by El Forum - 07-09-2012, 11:17 AM
Validation file input and text input - by El Forum - 07-10-2012, 01:23 AM
Validation file input and text input - by El Forum - 07-10-2012, 02:11 AM
Validation file input and text input - by El Forum - 07-10-2012, 12:03 PM
Validation file input and text input - by El Forum - 07-10-2012, 12:22 PM
Validation file input and text input - by El Forum - 07-11-2012, 01:44 AM
Validation file input and text input - by El Forum - 07-11-2012, 11:05 AM
Validation file input and text input - by El Forum - 07-11-2012, 02:29 PM
Validation file input and text input - by El Forum - 07-11-2012, 02:53 PM
Validation file input and text input - by El Forum - 07-11-2012, 02:58 PM
Validation file input and text input - by El Forum - 07-12-2012, 01:50 AM
Validation file input and text input - by El Forum - 07-12-2012, 02:17 AM
Validation file input and text input - by El Forum - 07-13-2012, 10:48 AM



Theme © iAndrew 2016 - Forum software by © MyBB