CodeIgniter Forums
check if a value already exists in database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forum-11.html)
+--- Thread: check if a value already exists in database (/thread-64002.html)



check if a value already exists in database - shanavas_m - 01-01-2016

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

Thanks in advance 



RE: check if a value already exists in database - skunkbad - 01-01-2016

See the documentation for making your own callback methods:

https://www.codeigniter.com/userguide3/libraries/form_validation.html#callbacks-your-own-validation-methods

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


RE: check if a value already exists in database - RobertSF - 01-02-2016

There is also a built-in validation rule for that: is_unique.
https://www.codeigniter.com/user_guide/libraries/form_validation.html#rule-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...


RE: check if a value already exists in database - skunkbad - 01-02-2016

(01-02-2016, 01:18 AM)RobertSF Wrote: There is also a built-in validation rule for that: is_unique.
https://www.codeigniter.com/user_guide/libraries/form_validation.html#rule-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.


RE: check if a value already exists in database - shanavas_m - 01-04-2016

Thanks for help.

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


RE: check if a value already exists in database - Diederik - 01-04-2016

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;
 
               }
 
       }





RE: check if a value already exists in database - shanavas_m - 01-04-2016

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

Thanks


RE: check if a value already exists in database - skunkbad - 01-04-2016

(01-04-2016, 08: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.


RE: check if a value already exists in database - Tecvid - 04-09-2016

(01-01-2016, 09: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]'
    
)
);