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]
  • #31 / Aug 13, 2009 6:45am

    quest13

    35 posts

    I am sorry to repeat.But my problem is not solved.I will write down step by step,please someone correct me where am I committing the mistake.

    1. I downloaded it from “http://devbro.com/testing/ci_form_validation/”.

    2. I created a file called “MY_Form_validation” and saved it in the libraries folder where the “Form_validation.php” is kept.

    3. I included this library in my controller like this

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

    4. My controller looks like this,

    $this->form_validation->set_rules("uploadedfile","File Upload","file_required|file_min_size[10KB]|file_max_size[500KB]|file_allowed_type[image]|file_image_mindim[50,50]|file_image_maxdim[400,300]");


    5. My view look like this,

    <tr>
        <td><input type="hidden" name="MAX_FILE_SIZE" value="100000" /><label>
        Select an Image to upload:</label></td>
    
        <td><input name="uploadedfile" type="file" 
           <?php echo set_value('uploadedfile'); ?> id="uploadedfile"
        /></td>
        </tr>
    
        <tr><td></td>
        <div class="error"><?php echo form_error('uploadedfile'); ?></div>
        </tr>

     

    But when I submit my form without loading any file, it doesnot falling into

    if ($this->form_validation->run() == FALSE){
    $this->Addimage($productid);
    }else {
    
    $data['result']=$this->product_model->uploadimage($productid);
    
    }

    it goes to else part instead.


    Where am I doing the mistake ?


    Any help regarding this will be extremely helpful to me as it will reduce many lines of unnecessary code.

    Thanks.

  • #32 / Aug 13, 2009 6:59am

    Chicken's Egg

    39 posts

    Dear quest13,

    As far as I can see, there are two mistakes in your code:
    1) MY_Form_validation is a library. The user guide clearly states that user libraries should be saved in ‘application/libraries’. The original Form_validation.php is kept in system/libraries.
    2) To load a libary that extends an existing library of CodeIgniter (like this one), you can simply use:

    $this->load->library('Form_validation');
  • #33 / Aug 13, 2009 7:17am

    quest13

    35 posts

    Thanks for your quick response.

    I already tried it.I placed this library (MY_Form_validation)under the application folder.

    But nothing happend.

    when I tried $this->Form_validation, it throws an error and didn’t recognise this at all.

    Thanks

  • #34 / Aug 13, 2009 5:50pm

    devbro

    81 posts

    1. create a file called MY_form_validation.php in application/libraries
    2. load it with

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

    3. the code for testing is this:

    if ($this->form_validation->run() === TRUE)
    {
    //success
    }
    else
    {
    //failed
    }

    the rest is right

    re-download the file one more time. there was a minor error that prevented it from working. It should be fixed now.

  • #35 / Aug 16, 2009 6:20am

    quest13

    35 posts

    Thanks a lot. I will re download it once again ( Is it from the same path you previously uploaded? ) and check it.

    Thanks again.

  • #36 / Aug 16, 2009 5:00pm

    devbro

    81 posts

    yes same page. I fixed something in version 2.

  • #37 / Aug 25, 2009 4:00am

    copernicus

    9 posts

    Thanks for the great code, one question. In my form I have two fields for uploading files that I want both to be required. The thing is if both are left empty I get two identical error messages. Is there a way to append a custom error message to the rule, depending on the field being validated?

  • #38 / Aug 25, 2009 4:09am

    devbro

    81 posts

    Thanks for the great code, one question. In my form I have two fields for uploading files that I want both to be required. The thing is if both are left empty I get two identical error messages. Is there a way to append a custom error message to the rule, depending on the field being validated?

    unfortunately that would be the case for normal fields in form_validation. You will need to modify the code to achieve that result.

  • #39 / Aug 27, 2009 2:46am

    alboyd

    120 posts

    Wow man - you are a legend. Thanks heaps for this - it’s brilliant.

    EDIT: It worked the first time but now my form validation is always evaluating to TRUE even when I have never opened the page.. WTF is up with that?

    EDIT: OK Now I have spent ages trying to figure out why this isn’t working. All I know is the following:

    1. My form has one field which is the “userfile” input.
    2. I load my page i get a blank page.
    3. I add a new validation rule for field “something” and set it to “required” then my page loads (with a validation error)

    ?? Am I being really stupid??

    EDIT EDIT EDIT:

    I have added the following to my form loading controller function and now it works fine.

    if (count($_POST)+count($_FILES) > 0)
            {
                $this->form_validation->set_rules('userfile', 'Poster Image', 'file_required|xss_clean|file_allowed_type[image]|file_size_max[2000]');
            }
  • #40 / Aug 27, 2009 1:51pm

    devbro

    81 posts

    I think I know what the problem is.

    The original form_validation class has a built in check that if $_POST is empty do nothing. it causes a problem if you only have $_FILES. So I went around it and added a dummy field to allow for $_FILES only validation.

  • #41 / Aug 27, 2009 7:59pm

    alboyd

    120 posts

    Yeh I noticed that. The problem is - without checking for count($_POST) > 0 before setting the validation rules, they are fired all the time. I noticed my other forms with nothing to do with FILES were displaying the errors even though the page had only just been loaded.

    Will there be a way to fix this or will I have to add that code to all my forms? I’ve got LOTS!

  • #42 / Aug 27, 2009 8:08pm

    alboyd

    120 posts

    Any reason you can’t change your code to this:

    if(count($_POST)===0 && count($_FILES) > 0)//it will prevent the form_validation from working
            {
                
                //add a dummy $_POST
                $_POST['DUMMY_ITEM'] = '';
                parent::set_rules($field,$label,$rules);
                unset($_POST['DUMMY_ITEM']);
            }

    And the same for the “run” function.

    It seems to work for me so far I haven’t found a problem with this?

  • #43 / Aug 28, 2009 7:08am

    devbro

    81 posts

    Any reason you can’t change your code to this:

    if(count($_POST)===0 && count($_FILES) > 0)//it will prevent the form_validation from working
            {
                
                //add a dummy $_POST
                $_POST['DUMMY_ITEM'] = '';
                parent::set_rules($field,$label,$rules);
                unset($_POST['DUMMY_ITEM']);
            }

    And the same for the “run” function.

    It seems to work for me so far I haven’t found a problem with this?

    I just incorporated that change into my code. 😉 it is now version 2.1

  • #44 / Sep 07, 2009 2:19am

    echoDreamz

    77 posts

    Does this work with, or replace the existing form validation library?

  • #45 / Sep 07, 2009 4:17am

    devbro

    81 posts

    Does this work with, or replace the existing form validation library?

    It extends, basically adds functionality to CURRENT form_validation library.

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

ExpressionEngine News!

#eecms, #events, #releases