Welcome Guest, Not a member yet? Register   Sign In
Duplicate entry check
#1

[eluser]mathinoz[/eluser]
Hello, I am new to codeigniter (and to programming) and i am having the following problem.

I have created my controller that lists the entries of the table, deletes an entry and of course adds an entry or edit an entry. I wanted to avoid having dublicate ('title') entries so i have created my own callback validation function to avoid this and this works fine when i add a new entry.. BUT there is a problem when i edit an entry.

When i add a new entry i can check succesfully if the new title already exist in my database, but I cannot use my callback function when i edit an entry cause it will always produce an error.

My callback function is the following

$rules['domain']= "callback_duplicate_entry"; - this is the way i call it in my controller

and the actual function is
function duplicate_entry()
{
$this->load->model('Mdomains', '', TRUE);
$checktitles['data'] = $this->Mdomains->getalldomains();
foreach ($checktitles['data'] as $v){
if ($v['domain'] == $this->input->post('domain'))
{
$this->validation->set_message('duplicate_entry', 'The domain name - ['.$this->input->post('domain').'] already exist.');

return FALSE;
}
}



SO as you can guess when i edit an entry, the title exists in the database and the callback function will ALWAYS produse an error.

Is the method i am using in the validation callback function correct? should i do it in a different way?
Any ideas?
Thanks
#2

[eluser]Johan André[/eluser]
Hey!

The dupecheck will always return that theres a duplicate entry since the post you currently editing is in the table and included in the check.

You should do:

SELECT 1 FROM table WHERE id != 'here goes the primary key of the post you are editing' AND name = 'the name to check for'

The query returns 1 if the name is found in any OTHER record other than the one being edited.

The problem is to get the id into the form validation callback function. I solved it by (in my edit method) setting $this->id to the id of the post being edited. Then you can use it in the callback function (as long as it is in the same controller ofcourse).

Good luck!
#3

[eluser]mathinoz[/eluser]
Thanks for the reply,
i thought of that before , but how do i pass the primary key into the callback function?
#4

[eluser]Johan André[/eluser]
In your controller, do:

Code:
function edit($id)
{
    // do your editing of post with id = $id here

    // Save the id to the controllers variables
    $this->edited_id = $id;
}

function callback_dupecheck($name)
{
    // Check for dupes
    $id = $this->edited_id;
}

Something like that.
#5

[eluser]mathinoz[/eluser]
Thanks !!! that worked !!!
I did not know that i could pass a variable to controller variables...
#6

[eluser]Johan André[/eluser]
No problem!

You got access to the variables of the extended classes too, you know!
It's the magic of OOP... Well, to some extent anyway! Smile




Theme © iAndrew 2016 - Forum software by © MyBB