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?

[code]
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]JanDoToDo[/eluser]
Anyone? Smile
#3

[eluser]John Pantoja[/eluser]
I'm sniffing the docs right now to see if I can catch anything. So far your file upload looks cookie cutter from the docs and should work. the if/else checks to see if the upload did work, if so calls the view with success. Are you getting any errors?
#4

[eluser]Colin Williams[/eluser]
Your code is pretty messy. There should be ZERO HTML markup in your controller. All of your $form .= '' stuff should just be $form = $this->load->view('my_form', $data_that_form_needs, TRUE);

You can use validation callbacks to attempt the file uploads and report your success or failure back to the form_validation class (all covered in the Form_validation docs)




Theme © iAndrew 2016 - Forum software by © MyBB