Validate form in codeigniter

1

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>
    
asked by Ing Alejandro Montes 04.05.2016 в 17:12
source

1 answer

2

Look, assuming you have set the .htacces to remove the index.php

we have the 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>
                  <?= form_error('nambre');?>
                </td>
                <td><input type="text" id="nombre" name="nombre" value="<?= set_value('nombre');?>"></td>
              </tr>
              <tr>
                <td><label><a>Telefono:</a></label></td>
              </tr>
              <tr>
                <td>
                  <?= form_error('telefono');?>
                </td>
                <td><input type="tel" id="telefono" name="telefono" value="<?= set_value('telefono');?>"></td>
              </tr>
              <tr>
                <td style="height:30px"><label><a>Email:</a></label></td>
              </tr>
              <tr>
                <td>
                  <?= form_error('email');?>
                </td>
                <td><input type="email" id="email" name="email" value="<?= set_value('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>
                  <?= form_error('asunto');?>
                </td>
                <td><input type="text" id="asunto" name="asunto" value="<?= set_value('asunto');?>"></td>
              </tr>
              <tr>
                <td><label><a>Mensaje:</a></label></td>
              </tr>
              <tr>
                <td>
                  <?= form_error('mensaje');?>
                </td>
                <td><textarea rows="04" id="mensaje" name="mensaje"><?= set_value('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>

controller

class Emails extends CI_Controller{
   public function __construct(){
    parent::__construct();

    $this->load->library(array('email', 'form_validation'));
  }

  function index(){
    $datos['contenido'] = 'emails';
    $this->load->view('contacto/mailcontacto', $datos); 
  }

  function enviar() {
    $this->form_validation->set_rules('nombre', 'Nombre', 'trim|required|min_length[3]|xss_clean');
    $this->form_validation->set_rules('telefono', 'Telefono', 'trim|required|exact_length[10]|xss_clean');
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
    $this->form_validation->set_rules('asunto', 'Asunto', 'trim|required|min_length[5]|xss_clean');
    $this->form_validation->set_rules('mensaje', 'Mensaje', 'trim|required|min_length[5]|xss_clean');

  if( $this->form_validation->run() === FALSE){
    $datos['contenido'] = 'emails';
    $this->load->view('contacto/mailcontacto', $datos); 
  }
  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
  }
}

I hope it will help you, if you have any errors send me the error that shows you

    
answered by 04.06.2016 / 01:02
source