• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
check if a value already exists in database

#1
How to check if a value already exists in database using built in form validation methods ?

Thanks in advance 
Reply

#2
See the documentation for making your own callback methods:

https://www.codeigniter.com/userguide3/l...on-methods

It is within one of these callback methods that you would simply query your database for an existing value.
Reply

#3
There is also a built-in validation rule for that: is_unique.
https://www.codeigniter.com/user_guide/l...-reference

Note that under CI version 2, if you chained validation rules, you could not put spaces between them.
PHP Code:
// works
$this->form_validation->set_rules('email''Email''required|is_unique[users.email]');
// doesn't work
$this->form_validation->set_rules('email''Email''required | is_unique[users.email]'); 
Usually, white space doesn't matter in code, but in this case, it does. I don't know if that's still the case in version 3, but just so you know...
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply

#4
(01-02-2016, 02:18 AM)RobertSF Wrote: There is also a built-in validation rule for that: is_unique.
https://www.codeigniter.com/user_guide/l...-reference

Note that under CI version 2, if you chained validation rules, you could not put spaces between them.
PHP Code:
// works
$this->form_validation->set_rules('email''Email''required|is_unique[users.email]');
// doesn't work
$this->form_validation->set_rules('email''Email''required | is_unique[users.email]'); 
Usually, white space doesn't matter in code, but in this case, it does. I don't know if that's still the case in version 3, but just so you know...

This is the easier way to do it, but if you want to have a customized error message then you need to use a callback, or do something creative with str_replace.
Reply

#5
Thanks for help.

I need the reverse of is_unique ie, the rule should return true if an entry exists in the database.
Reply

#6
Then you need to add a callback function:

PHP Code:
<?php

class Form extends CI_Controller {

 
       public function index()
 
       {
 
               $this->load->helper(array('form''url'));

 
               $this->load->library('form_validation');

 
               $this->form_validation->set_rules('username''Username''required|callback_exists_in_database');                
                $this
->form_validation->set_rules('password''Password''required|min_length[8]');

 
               if ($this->form_validation->run() == FALSE)
 
               {
 
                       $this->load->view('myform');
 
               }
 
               else
                
{
 
                       $this->load->view('formsuccess');
 
               }
 
       }

 
       public function exists_in_database($username)
 
       {

 
               $query $this->db->get_where('my_users', array('username' => $username)); 

 
               if ($query->num_rows() == )
 
               {
 
                       $this->form_validation->set_message('exists_in_database''Please enter an existing username');
 
                       return FALSE;
 
               }
 
               else
                
{
 
                       return TRUE;
 
               }
 
       }


Reply

#7
I know it can be done with callbacks.
Just looking for in-built validation methods.

Thanks
Reply

#8
(01-04-2016, 09:42 PM)shanavas_m Wrote: I know it can be done with callbacks.
Just looking for in-built validation methods.

Thanks

There are none, but if you are looking to re-use such a callback many times, consider adding it to a MY_Form_validation.php file.
Reply

#9
(01-01-2016, 10:30 PM)shanavas_m Wrote:
How to check if a value already exists in database using built in form validation methods ?

Thanks in advance 

yeah, i am late for a reply, but i have the simplest way to check if a value already exists, just extend form validation class by MY_Form_validation like this:

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
MY_Form_validation extends CI_Form_validation {

    public function 
is_exists($str$field)
    {
        return 
$this->is_unique($str$field) === FALSE;
    }



now, create form_validation_lang.php file in your language directory and:

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

$lang['form_validation_is_exists'] = 'The {field} field is already exists.'

that's all, now u can use in this way Smile

PHP Code:
$rules = array(
    array(
        
'field' => 'email',
        
'label' => 'lang:email',
        
'rules' => 'required|is_exists[users.email]'
    
)
); 
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.