CodeIgniter Forums
Problems with Form_Validation and Upload LIbraries - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problems with Form_Validation and Upload LIbraries (/showthread.php?tid=27442)



Problems with Form_Validation and Upload LIbraries - El Forum - 02-11-2010

[eluser]Sheikh Aman Alam[/eluser]
Hello.
i was wondering to know if there is a way to valida that-
a space wasn't put in the form field that has been posted.

i can use "alpha_dash", but i want to allow dot(.) and @ as well.

theres a segment in user guide that tells u can use php functions as validation function/rules too.
how to do that?


second thing, iam using upload library to upload images.
amazing library!
but is there any way to ignore the error if the user hasn't uploaded any image?
it makes the user to upload the image as a mandatory field, while that is not a requirement.

Please assist.


Problems with Form_Validation and Upload LIbraries - El Forum - 02-11-2010

[eluser]LuckyFella73[/eluser]
Hi,

Quote:i can use “alpha_dash”, but i want to allow dot(.) and @ as well.

In case you want to validate an email address you can use the
"valid_email" rule.
Otherwise you can write your own callback-functions additionally
to the build-in validation functions.
User Guide // Callback

About your file upload question:
Check if a file was selected for uploading like this:
Code:
if($_FILES['userfile']['size'])
{
     // do upload call
}



Problems with Form_Validation and Upload LIbraries - El Forum - 02-11-2010

[eluser]Sheikh Aman Alam[/eluser]
Thanks a lot for the link about callback functions.
it will work.
i'll try that soon.


Code:
if($_FILES['userfile']['size'])
{
     // do upload call
}

im using the 'upload' library of codeigniter, not PHP's core file upload mechanism,
i wanted to know in that.


Problems with Form_Validation and Upload LIbraries - El Forum - 02-11-2010

[eluser]LuckyFella73[/eluser]
Code:
if($_FILES['userfile']['size'])
{
     // do upload call
}

This line is just to check if a file was selected to upload.
If there is a file you go on with the codeigniter fileupload class!

A bit more detailed:
Code:
if($_FILES['userfile']['size'])
{
    // a file was selected so do the upload now
    // example from CI user guide:
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']    = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_form', $error);
    }    
    else
    {
        $data = array('upload_data' => $this->upload->data());
        $this->load->view('upload_success', $data);
    }
}



Problems with Form_Validation and Upload LIbraries - El Forum - 02-11-2010

[eluser]Sheikh Aman Alam[/eluser]
ohk.. so u mean to say that the library doesn't provide any way of doing so, i'll have to use php's methods.

rest of it i already know.
Thanks for ur help dear!


Problems with Form_Validation and Upload LIbraries - El Forum - 02-11-2010

[eluser]LuckyFella73[/eluser]
You're welcome!