[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());
}
}
//...
}