I am trying to validate a form in codeigniter and I have not been able to achieve it. I would like to validate the variables that arrive from the input of the form mailcontacto.php that is in view and that in that form show me the validation errors. The form sent well until you try to place the validation. Below the codes.
driver
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Emails extends CI_Controller {
function index(){
$datos['contenido'] = 'emails';
$this->load->view('contacto/mailcontacto', $datos);
}
function enviar() {
//Descargar la libreria
$this->load->library('email');
$this->load->library('form_validation');
$nombre = $this->input->post('nombre');
$telefono = $this->input->post('telefono');
$email = $this->input->post('email');
$asunto = $this->input->post('asunto');
$mensaje = $this->input->post('mensaje');
$body_msg = '<html><body><br />'.
'<h2><font face="times new roman" color="#da0021"><span><font face="times new roman" color="#00769f"> CONTACTO VIAJANDOFACIL.COM</h2></font>'.
'<table rules="all" style="border-width: 1px; border-style: dashed; border-color: #50a9d5; " cellpadding="10">' .
"<tr><td><strong>Nombre</strong> </td><td>" . $nombre . "</td></tr>".
"<tr><td><strong>Telefono:</strong> </td><td>" . $telefono . "</td></tr>".
"<tr style=style='background: #eee;'><td><strong>Enviado desde:</strong> </td><td>" . $email. "</td></tr>".
"<tr><td><strong>Asunto:</strong> </td><td>" . $asunto . "</td></tr>".
"<tr><td><strong>Mensaje:</strong> </td><td>" . $mensaje . "</td></tr>".
"<br />";
//Validaciones
//Nombre del campo, titulo, restricciones
$this->form_validation->set_rules('nombre', 'Nombre', 'required|min_length[3]|alpha|trim');
$this->form_validation->set_rules('email', 'Email', 'required|min_length[3]|valid_email|trim');
$this->form_validation->set_rules('telefono', 'Telefono', 'required|numeric');
$this->form_validation->set_rules('asunto', 'Asunto', 'required|min_length[3]|alpha|trim');
$this->form_validation->set_rules('mensaje', 'Mensaje', 'required|min_length[3]|alpha|trim');
if ($this->form_validation->run() == FALSE)
{
//Acción a tomar si existe un error el en la validación
}
else
{
//Acción a tomas si no existe ningun error
// Datos para enviar el correo
$this->email->from('[email protected]', 'Contacto');
$this->email->to('[email protected]');
$this->email->subject($asunto);
$this->email->message($body_msg );
$this->email->attach('img/logo.png');
$this->email->send();
redirect('contacto'); // Se direcciona
}
}
}
?>
view
<form action="emails/enviar" method="post">
<table style="width:80%; margin-left:12%">
<tbody>
<tr>
<td><table style="width:70%; margin-left:16%; margin-right:16%;">
<tbody>
<tr>
<td><label><a >Nombre:</a></label></td>
</tr>
<tr>
<td ><input type="text" id="nombre" name="nombre"></td>
</tr>
<tr>
<td><label><a>Telefono:</a></label></td>
</tr>
<tr>
<td><input type="text" id="telefono" name="telefono" ></td>
</tr>
<tr>
<td style="height:30px"><label><a>Email:</a></label></td>
</tr>
<tr>
<td><input type="text" id="email" name="email" ></td>
</tr>
</tbody>
</table>
</td>
<td><table style="width:100%">
<tbody>
<tr>
<td style="height:30px"><label><a>Asunto:</a></label></td>
</tr>
<tr>
<td><input type="text" id="asunto" name="asunto"></td>
</tr>
<tr>
<td><label><a>Mensaje:</a></label></td>
</tr>
<tr>
<td><textarea rows="04" id="mensaje" name="mensaje"></textarea></td>
</tr>
</tbody>
</table></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td><button type="submit" value="enviar" >Enviar</button></td><td></td>
</tr>
</tbody>
</table>
</form>