Welcome Guest, Not a member yet? Register   Sign In
Extended form_validation: now with file checking :)
#31

[eluser]quest13[/eluser]
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

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

4. My controller looks like this,

Code:
$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,


Code:
<tr>
    <td>&lt;input type="hidden" name="MAX_FILE_SIZE" value="100000" /&gt;&lt;label>
    Select an Image to upload:</label></td>

    <td>&lt;input name="uploadedfile" type="file"
       &lt;?php echo set_value('uploadedfile'); ?&gt; id="uploadedfile"
    /&gt;&lt;/td>
    </tr>

    <tr><td></td>
    <div class="error">&lt;?php echo form_error('uploadedfile'); ?&gt;</div>
    </tr>



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

Code:
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

[eluser]Chicken's Egg[/eluser]
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:
Code:
$this->load->library('Form_validation');
#33

[eluser]quest13[/eluser]
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

[eluser]devbro[/eluser]
1. create a file called MY_form_validation.php in application/libraries
2. load it with
Code:
$this->load->library('form_validation');

3. the code for testing is this:
Code:
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

[eluser]quest13[/eluser]
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

[eluser]devbro[/eluser]
yes same page. I fixed something in version 2.
#37

[eluser]copernicus[/eluser]
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

[eluser]devbro[/eluser]
[quote author="copernicus" date="1251201600"]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?[/quote]

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

[eluser]alboyd[/eluser]
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.
Code:
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

[eluser]devbro[/eluser]
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.




Theme © iAndrew 2016 - Forum software by © MyBB