ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Extended form_validation: now with file checking :)

July 21, 2009 7:36pm

Subscribe [25]
  • #46 / Oct 14, 2009 6:28pm

    dallen33

    88 posts

    Any reason why an error message isn’t being displayed when no file is selected?

    CONTROLLER

    $this->form_validation->set_rules('userfile','Image','file_required');

    VIEW

    
    <label for="userfile">Select Photo</label>
    <input type="file" name="userfile" id="userfile" />
    <?php if (form_error('userfile')): echo '<span class="error">'.form_error('userfile').'</span>'; endif; ?>
    
  • #47 / Oct 14, 2009 11:10pm

    devbro

    81 posts

    Any reason why an error message isn’t being displayed when no file is selected?

    CONTROLLER

    $this->form_validation->set_rules('userfile','Image','file_required');

    VIEW

    
    <label for="userfile">Select Photo</label>
    <input type="file" name="userfile" id="userfile" />
    <?php if (form_error('userfile')): echo '<span class="error">'.form_error('userfile').'</span>'; endif; ?>
    

    How are you opening your form? multipart?

  • #48 / Oct 14, 2009 11:30pm

    dallen33

    88 posts

    I’m using multipart. Also, when I use:

    <?php echo validation_errors(); ?>

    I get the error, but using the way I mentioned above brings no error. Weirdness…

  • #49 / Oct 15, 2009 12:40am

    devbro

    81 posts

    Thanks for letting me know. I will try to figure out why it happens and let you know.

  • #50 / Oct 15, 2009 1:08am

    dallen33

    88 posts

    Thanks, dude! This is an awesome extension btw. Thank you 😊

  • #51 / Oct 17, 2009 10:03pm

    dinhtrung

    63 posts

    How about localization? I suggest:

    function __construct()
        {
            parent::CI_Form_validation();
            $this->CI->load->lang("upload");
        }

    Then use language key instead of current error code.

    function file_upload_error_message($error_code)
        {
            switch ($error_code)
            {
                case UPLOAD_ERR_INI_SIZE:
                    return $this->CI->lang->line('upload_file_exceeds_limit');
                case UPLOAD_ERR_FORM_SIZE:
                    return $this->CI->lang->line('upload_file_exceeds_form_limit');
                case UPLOAD_ERR_PARTIAL:
                    return $this->CI->lang->line('upload_file_partial');
                case UPLOAD_ERR_NO_FILE:
                    return $this->CI->lang->line('upload_no_file_selected');
                case UPLOAD_ERR_NO_TMP_DIR:
                    return $this->CI->lang->line('upload_no_temp_directory');
                case UPLOAD_ERR_CANT_WRITE:
                    return $this->CI->lang->line('upload_unable_to_write_file');
                case UPLOAD_ERR_EXTENSION:
                    return $this->CI->lang->line('upload_stopped_by_extension');
                default:
                    return 'Unknown upload error';
            }
        }
  • #52 / Oct 19, 2009 6:12am

    DW@Van

    3 posts

    Hi devbro! Thanks for a great library. I have a question about setting rules. The rules are shown as below.

    'rules'   => 'file_allowed_type[image]|file_size_max[1800KB]'

    Notice that I didn’t set the file_required in my rule.

    When no file is selected, without file_required, the validation message still shows up. Is this the expected behaviour? Or, there is another way to make the file is an optional field, but if one is selected, still validates the type and size?

    UPDATE
    I find the follow comments in the code.

    // If the field is blank, but NOT required, no further tests are necessary

    The code followed after the comments seems missing a return statement so it will break out the function with no further processes. Therefore, I add the following code.

    else
    {
        return;
    }

    The whole block of code is shown below.

    // If the field is blank, but NOT required, no further tests are necessary
    $callback = FALSE;
    if ( ! in_array('file_required', $rules) AND $postdata['size']==0)
    {
        // Before we bail out, does the rule contain a callback?
        if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
        {
            $callback = TRUE;
            $rules = (array('1' => $match[1]));
        }
        else
        {
            return;
        }
    }

    The changes seem work for me. If someone uses this, please double test it. If the changes breaks the validation, please let me. I need to change it back.

  • #53 / Nov 02, 2009 6:13am

    CIfan

    11 posts

    Thanks for letting me know. I will try to figure out why it happens and let you know.

    Hi devbro,

    Thanks for the extension. I was wondering if you already found a way to display the single form_error() when no file is selected?

    Cheers

  • #54 / Nov 16, 2009 4:17pm

    djenniex

    14 posts

    Thanks for letting me know. I will try to figure out why it happens and let you know.

    Hi devbro,

    Thanks for the extension. I was wondering if you already found a way to display the single form_error() when no file is selected?

    Cheers

    Hey CIfan,

    If you want to get the single form_error function to work, you need to edit the _execute function to place all errors in the $this->_field_data[$row[‘field’]][‘error’].  Here is the code that I used from the Form_validation class.

    // Before doing anything check for errors
    if ($postdata['error'] !== UPLOAD_ERR_OK)
    {
        // Build the error message
        $message = sprintf($this->file_upload_error_message($postdata['error']), $this->_translate_fieldname($row['label']));
    
        // Save the error message
        $this->_field_data[$row['field']]['error'] = $message;
                    
        if ( ! isset($this->_error_array[$row['field']]))
        {
            $this->_error_array[$row['field']] = $message;
        }
        return FALSE;
    }

    This worked for me.

  • #55 / Nov 17, 2009 6:09am

    CIfan

    11 posts

    Thanks for letting me know. I will try to figure out why it happens and let you know.

    Hi devbro,

    Thanks for the extension. I was wondering if you already found a way to display the single form_error() when no file is selected?

    Cheers

    Hey CIfan,

    If you want to get the single form_error function to work, you need to edit the _execute function to place all errors in the $this->_field_data[$row[‘field’]][‘error’].  Here is the code that I used from the Form_validation class.

    // Before doing anything check for errors
    if ($postdata['error'] !== UPLOAD_ERR_OK)
    {
        // Build the error message
        $message = sprintf($this->file_upload_error_message($postdata['error']), $this->_translate_fieldname($row['label']));
    
        // Save the error message
        $this->_field_data[$row['field']]['error'] = $message;
                    
        if ( ! isset($this->_error_array[$row['field']]))
        {
            $this->_error_array[$row['field']] = $message;
        }
        return FALSE;
    }

    This worked for me.

    Thx for the reply. I placed your code directly under the _execute function in MY_Form_validation.php. When I submit the form, all my text input fields won’t validate and give the following form errors with no logic at all:
    - Unknown upload error
    - The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form

    Do you experience the same if you combine input text fields an input file fields?

  • #56 / Nov 17, 2009 12:24pm

    djenniex

    14 posts

    Thx for the reply. I placed your code directly under the _execute function in MY_Form_validation.php. When I submit the form, all my text input fields won’t validate and give the following form errors with no logic at all:
    - Unknown upload error
    - The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form

    Do you experience the same if you combine input text fields an input file fields?

    I’ve tested this in various ways, and all my inputs are validating including the file input fields and any text fields that I validate.

    What does your form_validation rules look like?

    Did you specify a MAX_FILE_SIZE in a hidden input field?  ie.

    <input type="hidden" name="MAX_FILE_SIZE" value="2048" />

    From the error output, it seems to functioning properly, if the file you are uploading is more than the max_file_size spec.

  • #57 / Nov 17, 2009 12:41pm

    CIfan

    11 posts

    Thx for the reply. I placed your code directly under the _execute function in MY_Form_validation.php. When I submit the form, all my text input fields won’t validate and give the following form errors with no logic at all:
    - Unknown upload error
    - The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form

    Do you experience the same if you combine input text fields an input file fields?

    I’ve tested this in various ways, and all my inputs are validating including the file input fields and any text fields that I validate.

    What does your form_validation rules look like?

    Did you specify a MAX_FILE_SIZE in a hidden input field?  ie.

    <input type="hidden" name="MAX_FILE_SIZE" value="2048" />

    From the error output, it seems to functioning properly, if the file you are uploading is more than the max_file_size spec.

    I added the hidden input field, but all the text fields will have the individual error: Unknown upload error

    Here are my form_validation rules:

    $this->form_validation->set_rules('title', 'lang:title', 'trim|required');
    $this->form_validation->set_rules('intro', 'lang:intro', 'trim|required');        
    $this->form_validation->set_rules('image', 'lang:image', 'file_required|xxs_clean|file_allowed_type[image]');
    $this->form_validation->set_rules('status', 'lang:status', 'required');

    If the title, intro, image and status values are empty the following codes will all give Unknown upload error as output.

    form_error('title')
    form_error('intro')
    form_error('image')
    form_error('status')

    Do you use this version? http://devbro.com/testing/ci_form_validation/code.php?v=2.1
    Can you post/upload a working example of your controller, view and custom library?

  • #58 / Nov 17, 2009 1:06pm

    djenniex

    14 posts

    Is xxs_clean a callback function?  Or did you mean to type xss_clean?

    Also, I did use version 2.1.  I’ve modified it a little bit, so that the function names were clear, removed a couple that were unnecessary, fixed some code that was a bit redundant and made some changes so that they conform more to the CodeIgniter Upload class.

    I’ve posted the code, here are the links:

    Test Controller
    Test View
    Custom Form_validation class

  • #59 / Dec 20, 2009 8:20am

    rebellion

    14 posts

    Hi devbro! Thanks for a great library. I have a question about setting rules. The rules are shown as below.

    'rules'   => 'file_allowed_type[image]|file_size_max[1800KB]'

    Notice that I didn’t set the file_required in my rule.

    When no file is selected, without file_required, the validation message still shows up. Is this the expected behaviour? Or, there is another way to make the file is an optional field, but if one is selected, still validates the type and size?

    UPDATE
    I find the follow comments in the code.

    // If the field is blank, but NOT required, no further tests are necessary

    The code followed after the comments seems missing a return statement so it will break out the function with no further processes. Therefore, I add the following code.

    else
    {
        return;
    }

    The whole block of code is shown below.

    // If the field is blank, but NOT required, no further tests are necessary
    $callback = FALSE;
    if ( ! in_array('file_required', $rules) AND $postdata['size']==0)
    {
        // Before we bail out, does the rule contain a callback?
        if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
        {
            $callback = TRUE;
            $rules = (array('1' => $match[1]));
        }
        else
        {
            return;
        }
    }

    The changes seem work for me. If someone uses this, please double test it. If the changes breaks the validation, please let me. I need to change it back.

    This didn’t work for me. Annoying to get an error when I haven’t set a requirement on the file upload.

  • #60 / Dec 21, 2009 4:15am

    Lulu Niu

    2 posts

    good.support!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases