Ver Mensaje Individual
  #1 (permalink)  
Antiguo 06/06/2011, 19:15
marlon5445
 
Fecha de Ingreso: julio-2010
Mensajes: 45
Antigüedad: 13 años, 10 meses
Puntos: 3
Validacion de Formulario en CodeIgniter

Buenas estoy trantode validar un forumario en codeigniter 2.02 pero no funciona correctamente.


Puse el campo nombre como requerido, sin embargo cuando envio el formulario con este campo vacio me acepta normal y no muestra ningun mesaje de error el codigo es el siguiente.

Controlador:


Código PHP:
<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of usuario
 *
 * @author Marlon
 */
class User extends CI_Controller {
    
//put your code here
    
        
function __construct(){
        
parent::__construct();
        
        
// load model
        
$this->load->model('userModel','',TRUE);
                
                
$this->load->helper('url','form');
                
                
$this->load->library('form_validation');
                
                
    }
        
        
        function 
index(){
        
         
        
        
// set common properties
        
$data['title'] = 'Agregar Nuevo Usuario';
        
$data['action'] = site_url('user/userAddRes');
    
        
// load view
        
$this->load->view('userAdd'$data);
        
    }
        
    

    
    function 
userAddRes(){
        
// set common properties
        
$data['title'] = 'Exito';
                
                
$this->form_validation->set_rules('nombre''nombre''required');
                
                if (
$this->form_validation->run() == FALSE)
        {
                    
$data['title'] = 'Agregar Nuevo Usuario';
                    
$data['action'] = site_url('user/userAddRes');
                    
$this->load->view('userAdd'$data);
                }
                else {
            
// save data
            
$user = array('codigoPucp' => $this->input->post('codigoPucp'),
                                      
'nombre' => $this->input->post('nombre'),
                                      
'apellidos' => $this->input->post('apellidos'),
                                      
'emailPucp' => $this->input->post('emailPucp'),
                                      
'sexo' => $this->input->post('sexo'),
                                      
'usuario' => $this->input->post('usuario'),
                                      
'password' => $this->input->post('password'),
                                      
'telefono' => $this->input->post('telefono'),
                                      
'mostrarTelefono' => $this->input->post('mostrarTelefono')
                                );
            
$id $this->userModel->save($user);
        
// load view
        
$this->load->view('userExito'$data);
                }
    }
    
}

?>


Vista:


Código PHP:
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><?php echo $title?></title>
    </head>
    <body>
      <!-- <form method="post" action="<?php //echo $action; ?>"> -->
        <?php echo form_open('user/userAddRes'); ?>
        <table align="center">

            <tr>
                            <td>Codigo PUCP:</td>
                            <td><input type="text" name="codigoPucp" /></td>
            </tr>
            <tr>
                            <td>Nombre:</td>
                            <td><input type="text" name="nombre" /></td>    
            </tr>
                        <tr>
                            <td>Apellidos:</td>
                            <td><input type="text" name="apellidos" /></td>    
            </tr>
                        <tr>
                            <td>Email PUCP:</td>
                            <td><input type="text" name="emailPucp" /></td>
            </tr>
                        <tr>
                            <td>Confirmar Email:</td>
                            <td><input type="text" name="confEmail" /></td>    
            </tr>
                        <tr>
                            <td><input type="radio" name="sexo" value="M" />Maculino</td>
                            <td><input type="radio" name="sexo" value="F" />Femenino</td>        
            </tr>
                        <tr>
                            <td>Usuario:</td>
                            <td><input type="text" name="usuario" /></td>
            </tr>
                        <tr>
                            <td>Contrase&ntilde;a:</td>
                <td><input type="password" name="password" /></td>    
            </tr>
                        <tr>
                            <td>Confirmar Contrase&ntilde;a:</td>
                            <td><input type="password" name="confPassword" /></td>
            </tr>
                        <tr>
                            <td>Tel&eacute;fono M&oacute;vil:</td>
                            <td><input type="text" name="telefono" /></td>
            </tr>
                        <tr>
                            <td><input type="checkbox" name="mostrarTelefono" value="S" />Deseo mostrar mi<br />
                                tel&eacute;fono m&oacute;vil</td>
            </tr>
                        <tr>
                            <td><input type="checkbox" name="aceptoCond" value="S" />Acepto la condiciones <br />
                                generales de uso</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" value="GUARDAR"/></td>
            </tr>
                </table>
                <?php echo form_close(); ?>
        <?php echo validation_errors(); ?>
    </body>
</html>
Modelo:

Código PHP:
<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of userModel
 *
 * @author Marlon
 */
class UserModel extends CI_Model {
    
    
//put your code here
    
    
    
private $usuario'usuario';
    
    function 
__construct(){
        
parent::__construct();
    }
    
    
    function 
save($user){
        
$this->db->insert($this->usuario$user);
        return 
$this->db->insert_id();
    }

}

?>
Espero que me puedan ayudar