1

I have create a form with 2 file upload fields and basically I want to make them required fields. I created a call back function but just can't seem to make it work properly. It is using the callback and posts the error if some other fields are left blank but sends the form whether files are attached or not. I'm pretty new to Codeigniter. Any help would be very much appreciated!

Controller:

class Form extends CI_Controller {

function index()
{
    $this->load->helper(array('form', 'url'));

    $this->load->library('form_validation');

    //Upload errors array
    $up_errors = array();

    $this->form_validation->set_rules('first_name', 'First Name', 'required|alpha');
    $this->form_validation->set_rules('last_name', 'Surname', 'required|alpha');
    $this->form_validation->set_rules('dob', 'Date of Birth', 'required');
    $this->form_validation->set_rules('nationality', 'Nationality', 'required|alpha');
    $this->form_validation->set_rules('gender', 'Gender', 'required');
    $this->form_validation->set_rules('address_l1', 'Address Line 1', 'required|alpha_dash_space');
    //$this->form_validation->set_rules('address_l2', 'Address Line 2', 'alpha');
    $this->form_validation->set_rules('address_city', 'City', 'required|alpha');
    $this->form_validation->set_rules('address_postcode', 'Post Code', 'required|alpha_dash_space');
    //$this->form_validation->set_rules('address_country', 'Country', 'required|alpha_dash_space');
    $this->form_validation->set_rules('e_address', 'Email Address', 'required|valid_email');
    $this->form_validation->set_rules('h_tel', 'Home Telephone Number', 'required|numeric');
    $this->form_validation->set_rules('mobile', 'Mobile Number', 'required|numeric');
    $this->form_validation->set_rules('university', 'University', 'required|alpha_dash_space');
    $this->form_validation->set_rules('campus', 'Campus Name', 'required|alpha_dash_space');
    $this->form_validation->set_rules('course', 'Course Title', 'required|alpha_dash_space');
    $this->form_validation->set_rules('end_date', 'Course End Date', 'required');
    //Custom callback
    $this->form_validation->set_rules('file', 'Attachment', 'callback_handle_upload');
    //Check if file attached
    //if (isset($_FILES['file']))
    //{


    //}

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('home');
    }
    else
    {

        //Display Success page
        $this->load->view('formsuccess');



        //Array helper
        $this->load->helper('array');





        //Set form data array
        $fields = $this->input->post('first_name')."\n".
        $this->input->post('last_name')."\n".
        $this->input->post('dob')."\n".
        $this->input->post('nationality')."\n".
        $this->input->post('gender')."\n".
        $this->input->post('address_l1')."\n".
        $this->input->post('address_l2')."\n".
        $this->input->post('address_city')."\n".
        $this->input->post('address_postcode')."\n".
        $this->input->post('address_country')."\n".
        $this->input->post('e_address')."\n".
        $this->input->post('h_tel')."\n".
        $this->input->post('mobile')."\n".
        $this->input->post('university')."\n".
        $this->input->post('campus')."\n".
        $this->input->post('course')."\n".
        $this->input->post('end_date');

        $msg = serialize($fields);

        //Upload files to server
        $this->load->library('upload');

        //Send Email
        $this->load->library('email');

        //Set config
        $config['upload_path']   = './attachments'; //if the files does not exist it'll be created
        $config['allowed_types'] = 'gif|jpg|png|doc|docx|txt|pdf';
        $config['max_size']   = '4000'; //size in kilobytes
        $config['encrypt_name']  = TRUE;

        $this->upload->initialize($config);

        $uploaded = $this->upload->up(FALSE); //Pass true if you want to create the index.php files


        //Attach the 2 files to email
        foreach($_FILES as $key => $value)
        {
            //var_dump($uploaded['success'][$key]['full_path']); //FOR TESTING
            $file = $uploaded['success'][$key]['full_path'];
            $this->email->attach($file);
            //unlink($file);
        }

        //var_dump($msg); //FOR TESTING
        $this->email->from($this->input->post('e_address'),$this->input->post('first_name') . $this->input->post('last_name'));
        $this->email->to('[email protected]'); 

        $this->email->subject('NON-SHU STUDENT REQUEST');
        $this->email->message($msg);    

        $this->email->send();

        //echo $this->email->print_debugger();
    }
}
function alpha_dash_space($str_in)
{
    if (! preg_match("/^([-a-z0-9_ ])+$/i", $str_in)) {
    $this->form_validation->set_message('_alpha_dash_space', 'The %s field may only contain alpha-numeric characters, spaces, underscores, and dashes.');
    return FALSE;
    } else {
    return TRUE;
}
}
 function handle_upload()
{
    if (count($_FILES['file']['name'] < 2)
    {
        // throw an error because nothing was uploaded
        $this->form_validation->set_message('handle_upload', "You must attach both files!");
        return false;
    }
    else
    {
        return TRUE;
    }
}

} ?>

1 Answer 1

1

check the following form validation extension which can help you to validate files, images with CI form validation library copy the code and create MY_Form_validation.php file in application library and paste code in that file and save

form file validation

code to copy

Sign up to request clarification or add additional context in comments.

2 Comments

the suggested link is not showing the demo proper. Have you any other link to lookout?
you can download code and try it your self its really working for file type input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.