Welcome Guest, Not a member yet? Register   Sign In
How to validate only one or two upload fields?
#1

[eluser]Filliped[/eluser]
Hello I come to ask a little help on the question.

I know how to upload using the upload library. No problem.
I know how to send email using the mail library and form validation. No problem.
And I also know how to add attachments to email. No problems too.

But if we put more of an upload field on the form and validate through the library upload
Code:
$this->upload->display_errors();
It compels all fields that have an attachment before the form is submitted.

How to validate only one or two upload fields in a form that contains multiple upload fields?

Thanks!
#2

[eluser]toopay[/eluser]
Show your recent code relating with that issues.
#3

[eluser]Filliped[/eluser]
This is my recent code:
Code:
public function send()
{
    $this->form_validation->set_message('required', 'Este campo é Obrigatório');
    
    $this->form_validation->set_rules('nome', 'Nome', 'trim|required|xss_clean');
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
    
    if($this->form_validation->run() == FALSE){
        $this->index();
    }
    else
    {
        $nome = $this->input->post('nome');
        $email = $this->input->post('email');
        
        $tabela  = "<table border=\"1\">
                    <tr>
                        <td>". $nome ."</td>
                        <td>". $email ."</td>
                    </tr>
                </table>";
                
        $this->email->set_newline("\r\n");
        $this->email->from('my-email', 'Filliped');
        $this->email->to('my-other-email');
        $this->email->subject('Teste de Email com o CodeIgniter');
        $this->email->message($tabela);
        
        for($i = 0; $i <= 2; $i++) {
        
            if($upload = $this->upload->do_upload('userfile'.$i)){
                if($upload === FALSE) continue;
                $data = $this->upload->data();
                $pathToUploadedFile[$i] = $data['full_path'];
                $this->email->attach($pathToUploadedFile[$i]);
            }
            else{
                echo($this->upload->display_errors());
                die;
            }

        }
                
        if($this->email->send()){
            $this->session->set_flashdata('msg', 'Email enviado com sucesso!');
            redirect('home', 'refresh');
        }else{
            show_error($this->email->print_debugger());
        }

    }
            
}

My Form:
Code:
&lt;?php echo form_open_multipart('home/send'); ?&gt;    
    <p>Nome:&lt;input type="text" name="nome" value="&lt;?php echo set_value('nome'); ?&gt;" /&gt;&lt;?=form_error('nome', ' ', ' ')?&gt;</p>
    <p>Email:&lt;input type="text" name="email" value="&lt;?php echo set_value('email'); ?&gt;"/&gt;&lt;?=form_error('email', ' ', ' ')?&gt;</p>
    &lt;input type="file" name="userfile1" /&gt;
    &lt;input type="file" name="userfile2" /&gt;
    <p>&lt;input type="submit" name="submit" /&gt;&lt;/p>
&lt;?php echo form_close(); ?&gt;
&lt;?php if($this->session->flashdata('msg')){ print_r($this->session->flashdata('msg')); } ?&gt;

My email.php file in the config folder is working.

These are the settings for my upload.php file that is in the config folder
Code:
$config['upload_path'] = './anexos/';
$config['allowed_types'] = 'doc|docx|xls|xlsx|pdf|jpg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1280';
$config['max_height'] = '1024';
$config['remove_spaces'] = true;
$config['overwrite'] = false;

Thanks
#4

[eluser]toopay[/eluser]
Just see below example...
Code:
public function send()
{
    //...

    // Validate files for upload section...
    $success = array();
    $errors = array();
    $updload_files = array(
            'userfile1' => $_FILES['userfile1'],
            'userfile2' => $_FILES['userfile2'],
            );
    
    foreach($updload_files as $field => $updload_file)
    {
        // Only process submitted file
        if($updload_file['error'] == 0)
        {
           // If there is an error, save it to error var
           if( ! $this->upload->do_upload($field) )
           {
               $errors[] = 'Failed to upload '.$field;  
           }
        
           // Use this to get the new file info if you need to insert it in a database
           $success[] = array( 'Success to upload '.$field => $this->upload->data());
        }
        else
        {
           $errors[] = $field . ' contain no data';  
        }
    }
    // Heres you could gettin know whats happen
    var_dump($errors);
    var_dump($success);
    
    // ...the rest of your code
}
#5

[eluser]Filliped[/eluser]
Hello toopay

I had to make was validated at least one attachment field independent of which were filled (or userfile1 or userfile2).
For this I did a count of errors and if greater than or equal to 2, had the error. (in this case are two fields with only one required).
In this case, this would be a good practice?

Thank you very very much for your help.
I was already a certain amount of time trying to resolve this and was not found.

I am also showing a different message in error, because if I do a foreach in array $errors, empty fields of attachments appear in the errors, even those who are not required and this can confuse the user.

See how my code was:
Code:
public function send()
{
    //...
        
    // Validate files for upload section...
    $success = array();
    $errors = array();
    $updload_files = array(
        'userfile1' => $_FILES['userfile1'],
        'userfile2' => $_FILES['userfile2'],
    );
        
    foreach($updload_files as $field => $updload_file)
    {
        // Only process submitted file
        if($updload_file['error'] == 0)
        {
            // If there is an error, save it to error var
            if( ! $this->upload->do_upload($field) )
            {
                $errors[] = 'Falha no upload do '.$field;
            }
            else
            {
                $data = $this->upload->data();
                $pathToUploadedFile= $data['full_path'];
                $this->email->attach($pathToUploadedFile);
            }
        }
        else
        {
            $errors[] = $field . ' não foi preenchido.';
        }
    }
        
    // Heres you could gettin know whats happen
    //var_dump($errors);
    //var_dump($success);
        
    if (count($errors) >= 2) {
        $data["failed"] = "Houve uma falha ao tentar enviar o email.";
        $this->load->view('home_view', $data);
    }
    else {
        if($this->email->send()){
            $this->session->set_flashdata('msg', 'Email enviado com sucesso!');
            redirect('home', 'refresh');
        }else{
            show_error($this->email->print_debugger());
        }
    }
    
    //...
    
}
#6

[eluser]toopay[/eluser]
[quote author="Filliped" date="1308943520"]
In this case, this would be a good practice?
[/quote]

Its fine.




Theme © iAndrew 2016 - Forum software by © MyBB