Welcome Guest, Not a member yet? Register   Sign In
Form Validation + Multiple Uploads
#1

[eluser]JanDoToDo[/eluser]
Hey guys,

I have just written a small form and have set validation rules up on it. I ahve also got two file upload inputs as well.
How Do i validate the normal forms, before the file uploads and then also validate the uploads worked? Here is my current code. Also, can i do multiple uploads on the same page?


function create($type = FALSE)
{
$this -> load -> library(‘form_validation’);
$this -> load -> helper(‘form’);
if ($type == ‘category’ || $type === FALSE) :
$rules = //Rules for all the fields (Couldnt include due to post limit)

$this -> form_validation -> set_rules($rules);

if ( $this -> form_validation -> run() === FALSE ) :
// Create the form to make a category
$form = form_open_multipart(‘admin/pages/create’);
$form .= ‘<span id=“title_span”>’;
$form .= form_error(‘title’);
$form .= form_label(‘Category Title’, ‘title_element’);
$data = array(
‘name’ => ‘title’,
‘id’ => ‘title_element’,
‘value’ => set_value(‘title’)
);
$form .= form_input($data);
$form .= ‘</span>’;
$form .= ‘<span id=“description_span”>’;
$form .= form_error(‘description’);
$form .= form_label(‘Category Description’, ‘description_element’);
$data = array(
‘name’ => ‘description’,
‘id’ => ‘description_element’,
‘value’ => set_value(‘description’)
);
$form .= form_input($data);
$form .= ‘</span>’;
$form .= ‘<span id=“upload_span”>’;
$form .= form_error(‘img’);
$form .= form_label(‘Category Image’, ‘img_element’);
$data = array(
‘name’ => ‘img’,
‘id’ => ‘img_element’,
‘value’ => set_value(‘img’)
);
$form .= form_upload($data);
$form .= ‘</span>’;
$form .= ‘<span id=“img_description_span”>’;
$form .= form_error(‘img_description’);
$form .= form_label(‘Image Description’, ‘img_description_element’);
$data = array(
‘name’ => ‘img_description’,
‘id’ => ‘img_description_element’,
‘rows’ => 8,
‘cols’ => 40,
‘value’ => set_value(‘img_description’)
);
$form .= form_textarea($data);
$form .= ‘</span>’;
$form .= ‘<span id=“upload_menu_span”>’;
$form .= form_error(‘menu_img’);
$form .= form_label(‘Menu Image’, ‘menu_img_element’);
$data = array(
‘name’ => ‘menu_img’,
‘id’ => ‘menu_img_element’,
‘value’ => set_value(‘menu_img’)
);
$form .= form_upload($data);
$form .= ‘</span>’;
$form .= ‘<span id=“submit_span”>’;
$form .= form_submit(‘submit_category’,‘Create!’);
$form .= ‘</span>’;
$form .= form_close();
// End form to make a category
else :
$this -> load -> model(‘pages_model’, ‘pages’);
// Upload the category image
$config[‘upload_path’] = image_url() . ‘categories/’;
$config[‘allowed_types’] = ‘gif|jpg|png’;
$config[‘max_size’] = ‘100’;
$config[‘max_width’] = ‘1024’;
$config[‘max_height’] = ‘768’;
$this -> load -> library (‘upload’, $config);

if ( ! $this -> upload -> do_upload(‘img’)) :
$error = array(‘error’ => $this -> upload -> display_errors());
//$this -> load -> view(‘upload_form’, $error);
else:
$data = array(‘upload_data’ => $this -> upload -> data());
//$this -> load -> view(‘upload_success’, $data);
endif;
#2

[eluser]John Pantoja[/eluser]
looks like you have two threads, if I understand from the docs, looks like this is a one at a time. Might need to nest a view to do multiple. In normal PHP I would loop over $_FILES to process each file. One thing I did notice is 'img' in do_upload, where is this from?

Sorry I'm having trouble following your code as I'm not that familiar with CI yet.

I'm assuming that CI's upload does a move on the uploaded file from the tmp.
#3

[eluser]John Pantoja[/eluser]
Maybe it's just me but it looks like your are combining everything into one function...

Some of what you have should be in a view and the validation/upload should be in a controller.




Theme © iAndrew 2016 - Forum software by © MyBB