CodeIgniter - File upload required validation

前端 未结 6 678
旧时难觅i
旧时难觅i 2020-12-01 02:08

I have 2 text fields and 1 file upload that are all required. Everything works when I require just the text fields, but when I require the file upload the validation error s

6条回答
  •  我在风中等你
    2020-12-01 02:38

    you can solve it by overriding the Run function of CI_Form_Validation

    copy this function in a class which extends CI_Form_Validation .

    This function will override the parent class function . Here i added only a extra check which can handle file also

    /**
     * Run the Validator
     *
     * This function does all the work.
     *
     * @access  public
     * @return  bool
     */
    function run($group = '') {
        // Do we even have any data to process?  Mm?
        if (count($_POST) == 0) {
            return FALSE;
        }
    
        // Does the _field_data array containing the validation rules exist?
        // If not, we look to see if they were assigned via a config file
        if (count($this->_field_data) == 0) {
            // No validation rules?  We're done...
            if (count($this->_config_rules) == 0) {
                return FALSE;
            }
    
            // Is there a validation rule for the particular URI being accessed?
            $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
    
            if ($uri != '' AND isset($this->_config_rules[$uri])) {
                $this->set_rules($this->_config_rules[$uri]);
            } else {
                $this->set_rules($this->_config_rules);
            }
    
            // We're we able to set the rules correctly?
            if (count($this->_field_data) == 0) {
                log_message('debug', "Unable to find validation rules");
                return FALSE;
            }
        }
    
        // Load the language file containing error messages
        $this->CI->lang->load('form_validation');
    
        // Cycle through the rules for each field, match the
        // corresponding $_POST or $_FILES item and test for errors
        foreach ($this->_field_data as $field => $row) {
            // Fetch the data from the corresponding $_POST or $_FILES array and cache it in the _field_data array.
            // Depending on whether the field name is an array or a string will determine where we get it from.
    
            if ($row['is_array'] == TRUE) {
    
                if (isset($_FILES[$field])) {
                    $this->_field_data[$field]['postdata'] = $this->_reduce_array($_FILES, $row['keys']);
                } else {
                    $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
                }
            } else {
                if (isset($_POST[$field]) AND $_POST[$field] != "") {
                    $this->_field_data[$field]['postdata'] = $_POST[$field];
                } else if (isset($_FILES[$field]) AND $_FILES[$field] != "") {
                    $this->_field_data[$field]['postdata'] = $_FILES[$field];
                }
            }
    
            $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
        }
    
        // Did we end up with any errors?
        $total_errors = count($this->_error_array);
    
        if ($total_errors > 0) {
            $this->_safe_form_data = TRUE;
        }
    
        // Now we need to re-set the POST data with the new, processed data
        $this->_reset_post_array();
    
        // No errors, validation passes!
        if ($total_errors == 0) {
            return TRUE;
        }
    
        // Validation fails
        return FALSE;
    }
    

提交回复
热议问题