Welcome Guest, Not a member yet? Register   Sign In
form validation - show upload errors
#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


Messages In This Thread
form validation - show upload errors - by Lykos22 - 10-14-2015, 08:29 AM
RE: form validation - show upload errors - by mwhitney - 10-14-2015, 08:52 AM



Theme © iAndrew 2016 - Forum software by © MyBB