Welcome Guest, Not a member yet? Register   Sign In
File upload validation
#31

[eluser]borgir[/eluser]
Hey there!
Sorry to bother you guys with the same issue...
I'm basing my validation on PistolPete's example and I'm always presented with the message:
"O campo Imagem é obrigatório." which means: "The image field is required."

Controller:
Code:
function enviar() {
        $this-> validate_form();
    }
    

     function validate_form() {
        
        $this->form_validation->set_error_delimiters('<p style="color:red;">', '</p>');

        $this->form_validation->set_rules('assunto', 'Assunto', 'trim|required|max_length[50]');
        $this->form_validation->set_rules('descricao', 'Descrição', 'trim|required');
        $this->form_validation->set_rules('userfile', 'Imagem', 'required|callback__do_upload');
        
        if ($this->form_validation->run() == FALSE) {
            $data['main_content'] = 'newsletter/newsletter_create_view';    
            $this->load->view('includes/template', $data);    
        } else {
            
        }
            
     }
    
    
     function _do_upload($file) {
        
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '3000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);
            
        if (!$this->upload->do_upload())
        {
            $this->form_validation->set_message('_do_upload', $this->upload->display_errors());
            return FALSE;
        } else {
            return TRUE;
        }
    }

View:
Code:
&lt;?php echo utf8_encode(form_error('assunto')); ?&gt;
&lt;?php echo utf8_encode(form_error('descricao')); ?&gt;
&lt;?php echo utf8_encode(form_error('userfile')); ?&gt;

&lt;form method="post" action="&lt;?php echo site_url() . '/newsletter/enviar'; ?&gt;" enctype="multipart/form-data"&gt;
    &lt;input type="text" name="assunto" value="&lt;?php echo set_value('assunto'); ?&gt;" size="60" /&gt;
    &lt;textarea name="descricao" rows="3" cols="74"&gt;&lt;?php echo set_value('descricao'); ?&gt;&lt;/textarea&gt;
    &lt;input type="file" name="userfile" /&gt;
    &lt;input type="submit" value="GUARDAR"&gt;
&lt;/form&gt;

Can anyone give me a hint?
Thks a lot!!
#32

[eluser]borgir[/eluser]
Made some progress here.
In the line:
Code:
$this->form_validation->set_rules('userfile', 'Imagem', 'required|callback__do_upload');
If I omit the 'required' parameter the callback function is performed. Otherwise the callback function doesn't event start.
How can I solve this?
Thks =)
#33

[eluser]Unknown[/eluser]
If I set rule: trim|required|callback__do_upload,
Although I upload image valid, but the error always set "The Image field is required."
Please help!
#34

[eluser]predat0r[/eluser]
Thanks pistolPete, your solution solved my upload validation! :-P

[quote author="pistolPete" date="1235453832"]Say we have a form which consists of a input field (called "image_title") and one file upload.
Both of them are required to pass the validation.

The rule looks like:
Code:
$this->form_validation->set_rules('image_title', 'Image title', 'trim|required|callback__do_upload');
Code:
// callback function to process the file upload and validate it
function _do_upload($file)
{
    $config['upload_path'] = './folder/';
    $config['allowed_types'] = 'put|your|allowed|types|here';
    $this->load->library('upload', $config);
        
    if ( ! $this->upload->do_upload())
    {
        // set the validation error using the errors provided by the upload class
        $this->form_validation->set_message('_do_upload', $this->upload->display_errors());
        return FALSE;
    }    
    else
    {
        return TRUE;
    }
}
[/quote]




Theme © iAndrew 2016 - Forum software by © MyBB