Welcome Guest, Not a member yet? Register   Sign In
Form validation with file upload
#1

[eluser]tokyotech[/eluser]
I have a form with a text input, a textarea, and file upload.

Code:
<?=form_open_multipart('club/open')?>
<div>
    <strong>title</strong>
    &lt;?=form_input('title', set_value('title'))?&gt;
    <em>&lt;?=form_error('title')?&gt;</em>
</div>
<div>
    <strong>description</strong>
    &lt;?=form_textarea('description', set_value('description'))?&gt;
    <em>&lt;?=form_error('description')?&gt;</em>
</div>
<div>
    <strong>thumbnail</strong>
    &lt;?=form_upload('thumbnail')?&gt;
    <em>&lt;?=form_error('thumbnail')?&gt;</em>
</div>
&lt;?=form_submit('submit', 'submit')?&gt;
&lt;?=form_close()?&gt;

The validation rules work well for the two text based inputs, but always extraneously says "the file field is required".

Code:
$this->form_validation->set_rules(
    'title',
    'title',
    'trim|required|min_length[2]|max_length[64]|xss_clean|htmlspecialchars|callback_titleIsUnique'
);

$this->form_validation->set_rules(
    'description',
    'description',
    'trim|required|min_length[3]|max_length[255]|htmlspecialchars'
);

$this->form_validation->set_rules(
    'thumbnail',
    'thumbnail',
    'required'
);

if ($this->form_validation->run()) {
    // TODO
}
else {
    $this->create();
}

So what's the best way to handle files with the form_validation class? I also wanted to use the upload class to check maximum filesize and whatnot, but it already seems like the form_validation class does not play well with files...
#2

[eluser]überfuzz[/eluser]
Quite right. You better off separating the form_validation and the upload. If the validation is passed continue with the upload.
#3

[eluser]tokyotech[/eluser]
I think I'll just do this:

Code:
// ....
$this->form_validation->set_rules(
    'thumbnail',
    'thumbnail',
    'callback_properThumbnail'
);
// ...

public function properThumbnail($dontCare)
{
    if (!isset($_FILES['thumbnail']) || $_FILES['thumbnail']['size'] <= 0) {
        $this->form_validation->set_message(
            'properThumbnail',
            'The %s is required'
        );
        return false;
    }
    else {
        return true;
    }
}
#4

[eluser]fRkSsK[/eluser]
i didn't work this code. i use this page for how to. but it doesn't give any error.
what is the problem?

Code:
&lt;?php

    function photo_check()
    {
        if (!isset($_FILES['h_photo'])) {
            $this->form_validation->set_message('photo_check', 'Upps!..');
            return true;
        }
        else {
            return false;
        }
    }
    
    function photo_add()
    {        
        $config = array(
               array('field' => 'h_photo'  ,'label' => 'Hook Photo'  ,'rules' => 'callback_photo_check'),            
            );
        $this->form_validation->set_rules($config);              
        
        if ($this->form_validation->run() == FALSE)
            {
                // bla bla...
            }
            else
            {
                // bla bla...
            }      
    }
?&gt;
#5

[eluser]fRkSsK[/eluser]
i found some solutions and i applied that. but still it doesn't work.
http://www.mahbubblog.com/php/form-valid...deigniter/
#6

[eluser]tokyotech[/eluser]
The callback should return TRUE if it validates. You have it mixed up. Just use the code I posted earlier!
#7

[eluser]fRkSsK[/eluser]
i changed it. but it still doesn't work. it doesn't care photo_check function.

Code:
&lt;?php

    function photo_check()
    {
        if (!isset($_FILES['h_photo'])) {
            $this->form_validation->set_message('photo_check', 'Upps!..');
            return false;
        }
        else {
            return true;
        }
    }
    
    function photo_add()
    {        
        $config = array(
               array('field' => 'h_photo'  ,'label' => 'Hook Photo'  ,'rules' => 'callback_photo_check'),            
            );
        $this->form_validation->set_rules($config);              
        
        if ($this->form_validation->run() == FALSE)
            {
                // bla bla...
            }
            else
            {
                // bla bla...
            }      
    }
?&gt;
#8

[eluser]fRkSsK[/eluser]
i got it..
Code:
if (!is_uploaded_file($_FILES['h_foto']['tmp_name']))




Theme © iAndrew 2016 - Forum software by © MyBB