Posted By

manteca on 12/29/09


Tagged

form validation codeigniter


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

AzizLight
guillermoj88gmailcom


Extender validacion de Codeigniter


 / Published in: PHP
 

URL: http://net.tutsplus.com/tutorials/php/6-codeigniter-hacks-for-the-masters/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+nettuts+%28NETTUTS%29

Esto es para agregar la validacion de un nombre unico.

$this->formvalidation->setrules('username', 'Username', 'required|alphanumeric|minlength[6]|unique[User.username]');

Esto revisas que no exista un usuario (unique[User.username] ) en la table "User" ela columna "username".

  1. //Create: "application/libraries/MY_Form_validation.php"
  2. class MY_Form_validation extends CI_Form_validation {
  3.  
  4. function unique($value, $params) {
  5.  
  6. $CI =& get_instance();
  7. $CI->load->database();
  8.  
  9. $CI->form_validation->set_message('unique',
  10. 'The %s is already being used.');
  11.  
  12. list($table, $field) = explode(".", $params, 2);
  13.  
  14. $query = $CI->db->select($field)->from($table)
  15. ->where($field, $value)->limit(1)->get();
  16.  
  17. if ($query->row()) {
  18. return false;
  19. } else {
  20. return true;
  21. }
  22.  
  23. }
  24. }

Report this snippet  

You need to login to post a comment.