• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validating upload form field mixed with other fields

#1
[eluser]jleequeen[/eluser]
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?

Thanks.

#2
[eluser]tribalab[/eluser]
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?

thx!

a

#3
[eluser]ebot[/eluser]
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

#4
[eluser]abmcr[/eluser]
A rapyd copy/paste from my code....Send me if you don't understand
Ciao
Code:
function testi_edit()
        {    
            //librerie
            $this->load->library('form_validation');
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            $this->load->library('upload');
            //model for loading data from db
            $this->load->model("welcome_model","modello");
            
            //validatori


            $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
            $error=array();
            //upload the files
            foreach($_FILES as $key=>$value )
                  {
                if ( ! $_FILES[$key]['size']==0){
                    //carica il config del caso
                    $config="config_$key";
                    $this->upload->initialize($$config);
                    //go with the update
                    if ( ! $this->upload->do_upload($key))
                        {
                            $error[$key]= $this->upload->display_errors();
                        }else{
                            $data_file=($this->upload->data());
                            $this->db->set($key,$data_file['file_name']);
                            
                        }
                }
            }
            
            //avvia o il form o la scrittura sul db
            if (($this->form_validation->run() == FALSE)||(count($error)>0)){
                $data['error']=$error;
                

                $data["content"]=$this->load->view("form",$data,TRUE);
                
                //vista finale
                $this->load->view('admin/tema',$data);
            }
            else
            {
                //inserisce nel db
                
                $this->db->set('rel_categoria',$this->input->post("rel_categoria"));
                $this->db->set('id_padre',$this->input->post("id_padre"));
                $this->db->set('livello',$this->input->post("livello"));
                $this->db->set('menu_pagine_aggiunte',$this->input->post("menu_pagine_aggiunte"));
                $this->db->set('master',$this->input->post("master",0));
                $this->db->set('pagine',$this->input->post("pagine",0));
                $this->db->set('data',$this->input->post("data","00-00-0000 00:00:00"));
                
                
                $this->db->where('id', $data['id']);
                $this->db->update('testi');
                
                //flash message
                $this->session->set_flashdata('msg_edit', 'I dati sono stati salvati con successo');
                $this->session->set_flashdata('color_msg_edit', 'green');
                    
                redirect("ad/testi");    
            }
        }


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

IN the view
Code:
//this function is in an extended helper
function form_error_upload($chiave,$errori){
    return(array_key_exists($chiave,$errori)?$errori[$chiave]:NULL);
}

...
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";


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.