• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
form validation - show upload errors

#1
I'd like some help please.

I', having a contact form in which I can also upload files. I have set up the form validation and the uploading configuration according to the documentation, however I have spotted a "logic" problem.

If I submit all the form fields with valid data and select an invalid type file to upload, the form process works as correct, and redirects to my "thank you" page, whereas I should get the error message from the invalid file upload. I 'm assuming this is because the upload errors are not part of the form_validation library or the two libraries don't "communicate" some way.

How can I fix this bug ?

This is my code

PHP Code:
protected $rules = array(
        
'name' => array(
            
'field' => 'name',
            
'label' => 'Name',
            
'rules' => 'trim|required|min_length[2]',
        ),
        
'email' => array(
            
'field' => 'email',
            
'label' => 'Email address',
            
'rules' => 'trim|required|valid_email',
        ),
        
'phone' => array(
            
'field' => 'phone',
            
'label' => 'Phone number',
            
'rules' => 'trim|required',
        ),
        
'comments' => array(
            
'field' => 'comments',
            
'label' => 'Comments',
            
'rules' => 'trim|required|min_length[10]',
        ),
    );


// in my method, where I run the validation
if ($topic == 'Employment query' && count($_FILES)) {
            
    
$config['max_size'] = '5000';
    
$config['upload_path'] = 'content/cvs';
    
$config['allowed_types'] = 'doc|docx|pdf|odt';
    
$this->load->library('upload'$config);

    if (
$this->upload->do_upload('upload_cv')) {
        
$cv_file $this->upload->data();
    } 
    else {
        
$this->data['upload_error'] = $this->upload->display_errors('<span class="help-block">''</span>');
    }
}

$this->form_validation->set_rules($this->rules);
if (
$this->form_validation->run()) {
          
// get form data and send email
          
redirect('contact/thank_you');

Reply

#2
If you want your validation of the file upload to be part of the form validation, you should create a validation rule to perform the file upload/validation. In other words, you could do something like this:

PHP Code:
    protected function where_you_run_the_validation()
    {
        // in my method, where I run the validation
        if ($topic == 'Employment query') {
            $this->rules['upload_field'] = array(
                'field' => 'upload_field',
                'label' => 'upload field',
                'rules' => 'callback__validate_upload'// note double underscore
            );
        } else {
            unset($this->rules['upload_field']);
        }

        $this->form_validation->set_rules($this->rules);
        if ($this->form_validation->run()) {
            // get form data and send email
            redirect('contact/thank_you');
        }
    }

    
// underscore prefix prevents the CI router from routing this method, even though it's public
    public function _validate_upload($str)
    {
        if (count($_FILES)) {
            $config['max_size'] = '5000';
            $config['upload_path'] = 'content/cvs';
            $config['allowed_types'] = 'doc|docx|pdf|odt';
            $this->load->library('upload'$config);

            if ( ! $this->upload->do_upload('upload_cv')) {
                $this->form_validation->set_message('_validate_upload'$this->upload->display_errors('<span class="help-block">''</span>'));
                return false;
            

            // Note this is now a property instead of a variable
            $this->cv_file $this->upload->data();
        }

        return true;
    
Reply

#3
@mwhitney Thank you for your help! I tried it and its working! I didn't think of using callbacks, actually I started doing this using sessions, so this saved me some time!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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