Welcome Guest, Not a member yet? Register   Sign In
[ask] how upload and validate the form
#1

[eluser]siubie[/eluser]
hi guys iam trying to validate a form here consist of three data two of them are text and the last is a image file.
so far when uploading the image file its return an error "You did not select a file to upload."

here is my controller :
Code:
function addSponsor(){
        $data['judul']=$this->Title;
        $data['heading']="Penambahan Sponsor";
        $data['heading2']="Penambahan Sponsor Pimnas 2009";
        $data['nama']=array(
            'name'=>'nama'
       );
        $data['gambar']=array(
            'name'=>'gambar',
            'type'=>'file'
        );
        $data['url']=array(
            'name'=>'url'
        );
        $this->form_validation->set_rules('nama', 'Nama Sponosr', 'trim|required|xss_clean');
        $this->form_validation->set_rules('gambar', 'Gambar Harus Di Isi', 'callback__userfile');
        $this->form_validation->set_rules('url', 'Link Ke Sponsor', 'trim|required');
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('controlPanelAdmin/addSponsor.php',$data);
        }
         else{
                            $masukkan=$this->pimnas_model->masukkanSponsor();
                                if($masukkan){
                                  $data['sukses']="Katalog Berhasil Dimasukkan";
                              }
                              else{
                                $data['sukses']="Katalog Gagal Dimasukkan";
                              }
                            $this->load->view('controlPanelAdmin/succedSponsor',$data);

                        }

        }

        function _userfile(){
        if (!$this->upload->do_upload())
        {
            $this->form_validation->set_message('_userfile',$this->upload->display_errors());

            return false;
        } else {
            return true;
        }
    }

is iam wrong with using a callback ?
#2

[eluser]pistolPete[/eluser]
Where do you set your upload configuration?

copied from the userguide:
Code:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']    = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';

Is your upload directory writeable?
Is your form field called "userfile"?

Also have a look at this thread: http://ellislab.com/forums/viewthread/94736/
#3

[eluser]siubie[/eluser]
i have change my code to this one :
Code:
function addSponsor(){
        $data['judul']=$this->Title;
        $data['heading']="Penambahan Sponsor";
        $data['heading2']="Penambahan Sponsor Pimnas 2009";
        $data['nama']=array(
            'name'=>'nama'
       );
        $data['gambar']=array(
            'name'=>'gambar',
            'type'=>'file'
        );
        $data['url']=array(
            'name'=>'url'
        );
        $this->form_validation->set_rules('nama', 'Nama Sponosr', 'trim|required|xss_clean');
        $this->form_validation->set_rules('gambar', 'Gambar Harus Di Isi', 'callback__gambar');
        $this->form_validation->set_rules('url', 'Link Ke Sponsor', 'trim|required');
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('controlPanelAdmin/addSponsor.php',$data);
        }
         else{
                            $masukkan=$this->pimnas_model->masukkanSponsor();
                                if($masukkan){
                                  $data['sukses']="Katalog Berhasil Dimasukkan";
                              }
                              else{
                                $data['sukses']="Katalog Gagal Dimasukkan";
                              }
                            $this->load->view('controlPanelAdmin/succedSponsor',$data);

                        }

        }

        function _userfile(){
        $config['upload_path'] = './asset/img/sponsor/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $this->upload->initialize($config);
        if (!$this->upload->do_upload('gambar'))
        {
            $this->form_validation->set_message('_gambar',$this->upload->display_errors());

            return false;
        } else {
            return true;
        }
    }
no error but the file not uploaded to ./asset/img/sponsor i am using windows and xampp 1.6.4
and when i add validation rule
Code:
$this->form_validation->set_rules('gambar', 'Gambar Harus Di Isi', 'trim|required|callback__gambar');
the result is this form not valid
#4

[eluser]TheFuzzy0ne[/eluser]
[url="http://ellislab.com/forums/viewthread/48552/"]This thread[/url] should help you.
#5

[eluser]siubie[/eluser]
thanks a lot guys its help me a lot Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB