• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form validation and file upload

#1
[eluser]brixxster[/eluser]
Hello guys,

I have this function which includes 3 form fields and one file upload field.

I wanted to validate the 3 form fields and check if the file upload field is properly filled and if the file being uploaded does not break any rules that I specify in the configuration.

Now the form field validation looks good but I could not get the upload file field validation to work.

THE CONTROL

Code:
function savePassport(){
        
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        
        $this->form_validation->set_rules('passport_num', 'Passport number', 'trim|required');
        $this->form_validation->set_rules('passport_issued', 'Passport issued', 'trim|required');
        $this->form_validation->set_rules('passport_exp', 'Passport expiration', 'trim|required');
        
            
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '1024';
        
        $this->load->library('upload', $config);
        
        if($this->form_validation->run() == FALSE || !$this->upload->do_upload() == TRUE){
            $data['error'] = array('error' => $this->upload->display_errors('<span class="error">', '</span>'));
            $data['applicant_content'] = '/applicant/updoc/passport';
            $data['headTitle']='Upload Passport';
            $this->load->view('includes/upload_template_view', $data);

        } else {
            echo "save";
            die();
        }
    }


THE VIEW

Code:
&lt;?php echo form_open_multipart(site_url() . '/applicant/savePassport');?&gt;
  <table border="0" cellspacing="5" cellpadding="5">
    <tr>
      <td valign="top">Passport number: </td>
      <td>&lt;?php echo form_input('passport_num', $this->input->post('passport_num'), 'size="30"');?&gt; &lt;?php echo form_error('passport_num'); ?&gt;</td>
    </tr>
    <tr>
      <td valign="top">Issued: </td>
      <td>&lt;?php echo form_input('passport_issued', $this->input->post('passport_issued'), 'size="30"');?&gt; &lt;?php echo form_error('passport_issued'); ?&gt;</td>
    </tr>
    <tr>
      <td valign="top">Expiration:</td>
      <td>&lt;?php echo form_input('passport_exp', $this->input->post('passport_exp'), 'size="30"');?&gt; &lt;?php echo form_error('passport_exp'); ?&gt;</td>
    </tr>
    <tr>
      <td valign="top">Upload passport:</td>
      <td>&lt;input type="file" name="userfile" /&gt; &lt;?php echo $error['error'];?&gt;
      </td>
    </tr>
    <tr>
      <td valign="top">&nbsp;</td>
      <td>&lt;input name="acct_id" type="hidden" id="acct_id" value="&lt;?php echo $this-&gt;session->userdata['acct_id']; ?&gt;" />&lt;?php echo form_submit('submit', 'Save'); ?&gt;</td>
    </tr>
  </table>
  &lt;?php echo form_close();?&gt;

I might be missing something here? Please help...

Thanks in advanced.

#2
[eluser]brixxster[/eluser]
Hello guys,

Just wanted to know what you did in validating form fields and the same time validating the image upload field or can this be done?

Code:
if($this->form_validation->run() == FALSE) || !$this->upload->do_upload() == TRUE ){
    'call back form page';
}

Thanks in advance.

#3
[eluser]mohsin917[/eluser]
Try this one

if($this->form_validation->run() == FALSE) || !$this->upload->do_upload('userfile') == TRUE ){
'call back form page';
}

#4
[eluser]brixxster[/eluser]
Hello mohsin917,

Thanks for the help but it didn't work. I'm still using Elseif to validate the two... it works but it validates the the regular fields and the userfile fields separate. I wish there is a way to validate the regular and the file upload field in one line like the one we're discussing above.

I'm sure someone has encountered this problem before. Anyone?

Thanks

#5
[eluser]RobertB.[/eluser]
Quote:function savePassport(){

$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');

$this->form_validation->set_rules('passport_num', 'Passport number', 'trim|required');
$this->form_validation->set_rules('passport_issued', 'Passport issued', 'trim|required');
$this->form_validation->set_rules('passport_exp', 'Passport expiration', 'trim|required');


$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '1024';

$this->load->library('upload', $config);

if($this->form_validation->run() == FALSE || !$this->upload->do_upload() == TRUE){
$data['error'] = array('error' => $this->upload->display_errors('<span class="error">', '</span>'));
$data['applicant_content'] = '/applicant/updoc/passport';
$data['headTitle']='Upload Passport';
$this->load->view('includes/upload_template_view', $data);

} else {
echo "save";
die();
}
}

CONTROLLER
Code:
function savePassport(){
        
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        
        $this->form_validation->set_rules('passport_num', 'Passport number', 'trim|required');
        $this->form_validation->set_rules('passport_issued', 'Passport issued', 'trim|required');
        $this->form_validation->set_rules('passport_exp', 'Passport expiration', 'trim|required');
        
            
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '1024';
        
        $this->load->library('upload', $config);
        
        if($this->form_validation->run() == FALSE || $this->upload->do_upload() == FALSE){
            $data['applicant_content'] = '/applicant/updoc/passport';
            $data['headTitle']='Upload Passport';
            $this->load->view('includes/upload_template_view', $data);

        } else {
            echo "save";
            die();
        }
    }

VIEW
Code:
&lt;?=form_open_multipart('controller')?&gt;                        
&lt;?=form_label('Passport number :', 'passport_num')?&gt;&lt;?=form_input('passport_num')?&gt;
&lt;?=form_error('passport_num')?&gt;
<div class="clear"></div>
&lt;?=form_label('Passport Issued :', 'passport_issued')?&gt;&lt;?=form_input('passport_issued')?&gt;
&lt;?=form_error('passport_issued')?&gt;
<div class="clear"></div>
&lt;?=form_label('Photo :', 'photo')?&gt;&lt;?=form_upload('userfile')?&gt;
&lt;?=$this->upload->display_errors()?&gt;


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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