PHP - Validar formulario en codeigniter

 
Vista:
Imágen de perfil de Alejandro

Validar formulario en codeigniter

Publicado por Alejandro (1 intervención) el 16/05/2016 02:59:10
Estoy tratando de validar un formulario en codeigniter y no he podido lograrlo. Me gustaría validar las variables que llegan de los input del formulario mailcontacto.php que se encuentra en view y que en ese formulario me muestre los errores de validación. El formulario envíaba bien hasta que le intente colocar la validación. A continuación los codigos.


CONTROLADOR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?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('desde@correo.com', 'Contacto');
        $this->email->to('a@correo.com');
        $this->email->subject($asunto);
        $this->email->message($body_msg );
        $this->email->attach('img/logo.png');
 
   $this->email->send();
   redirect('contacto'); // Se direcciona
   }
  }
 }
?>



VISTA

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<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>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder