Welcome Guest, Not a member yet? Register   Sign In
Validating upload form field mixed with other fields

Hello all,

I have a form that has a few text fields and also needs to have a field to upload a file. I am having a hard time looking at the docs and figuring out how to do this and have validation on the whole form. I would like to stay within the form validation class and not have to hack something if possible. Does the validation class and uploading class not work together?

Any ideas?


I'm having this issue now, the new validation class is nice, but I need to require that a file is also part of the mix. What's the cleanest way to do this?



hi its possible i will do something for and mail tomorrow, it was i have been using pretty simple not complicated. i post that tomorrow

A rapyd copy/paste from my code....Send me if you don't understand
function testi_edit()
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            //model for loading data from db

            $this->form_validation->set_rules('txtitaliano', 'Testo italiano', 'required');
            //this line for enablin the requeired file input
            //$this->form_validation->set_rules('prova_file', 'prova_file', 'callback_file_check');
            //recupera i dati del record
            $cid = (int) $this->input->post('id');
            $data['id'] = ($cid) ? $cid : $this->uri->segment($this->uri->total_segments());
            $data['row'] = $this->modello->get_data($data['id'],"testi");    
            //config for a file
            $config_prova_file['upload_path'] = './uploads/';
            $config_prova_file['allowed_types'] = 'gif|jpg|png|pdf';
            $config_prova_file['max_size']    = '1000';
            $config_prova_file['max_width']  = '1024';
            $config_prova_file['max_height']  = '768';
            $config_prova_file['thumb']  = '100|100';
            //imposta array per messaggi errore
            //upload the files
            foreach($_FILES as $key=>$value )
                if ( ! $_FILES[$key]['size']==0){
                    //carica il config del caso
                    //go with the update
                    if ( ! $this->upload->do_upload($key))
                            $error[$key]= $this->upload->display_errors();
            //avvia o il form o la scrittura sul db
            if (($this->form_validation->run() == FALSE)||(count($error)>0)){

                //vista finale
                //inserisce nel db
                $this->db->set('data',$this->input->post("data","00-00-0000 00:00:00"));
                $this->db->where('id', $data['id']);
                //flash message
                $this->session->set_flashdata('msg_edit', 'I dati sono stati salvati con successo');
                $this->session->set_flashdata('color_msg_edit', 'green');

    * callback per file required
    function file_check($str)
            if (($str == '')&&(($_FILES['foto']['size'])==0))
                $this->form_validation->set_message('file_check', 'The %s field can not be empty');
            return $flag;

IN the view
//this function is in an extended helper
function form_error_upload($chiave,$errori){

echo br();
    echo form_error_upload("prova_file",$error);echo "\n";
    echo form_hidden("prova_file",$row->prova_file);echo "\n";
    echo form_label('Allegato', 'prova_file');echo "\n";
    echo form_upload("prova_file",array("size"=>30));echo "\n";

Theme © iAndrew 2016 - Forum software by © MyBB