Im running CodeIgniter for a project of mine ... Im fairly new to this, i have the standard form validation checks for my form, except im unsure of how to check if the value already exists in the database.
This is what i have for my form validation rules so far
$this->form_validation->set_rules('rolename', 'Role Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('rolekey', 'Role Key', 'trim|required|xss_clean');
The 'rolekey' is what i need to check against the 'rolekey' column in the database to see if it exists, if it does i need to shoot back an error.
Any help would be grand.
Cheers,
There is not a built-in form validation check for whether or not a value is in the database, but you can create your own validation checks.
In your controller, create a method similar to this:
function rolekey_exists($key)
{
$this->roles_model->role_exists($key);
}
And in your model that handles roles, add something like this:
function role_exists($key)
{
$this->db->where('rolekey',$key);
$query = $this->db->get('roles');
if ($query->num_rows() > 0){
return true;
}
else{
return false;
}
}
And then you can write a form validation check like this:
$this->form_validation->set_rules('username', 'Username', 'callback_rolekey_exists');
See this page for more information: