⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

is_unique validation problem on edit



carlosT

carlosT
  • profile picture
  • Member

Posted 15 November 2012 - 20:38 PM

Hi Johnny, First of all 1.3 is soooo awesome, big pat on the back for you my friend!!! Loving the where clause in relations, Export, Print simply awesome!

I have run in to a kind of strange problem and in a way its perfectly logical but i don't know how i can get around it.

$crud->set_rules('email', 'Email', 'required|valid_email|is_unique[contacts.email]');


If I use this then when i go to edit a contact I can not save it because the email is already in the database so therefore its not unique.

I guess a custom rule would do the trick but its a bit of a hassle. Any ideas ?

victor

victor
  • profile picture
  • Member

Posted 15 November 2012 - 22:27 PM

HI!
try this:


....

if(!empty($this->input->post())
{
if($crud->getState()=='edit')
{
$user_data = $this->db->get_where('your_users_table',array('your_user_id_field'=>$this->input->post('yuur_user_id_field')))->row_array();

if($user_data['email']!=$this->input->post('email'))
{
$crud->set_rules('email', 'Email', 'required|valid_email|is_unique[contacts.email]');
}
}
else
{
$crud->set_rules('email', 'Email', 'required|valid_email|is_unique[contacts.email]');
}
}
...
$crud->render();


carlosT

carlosT
  • profile picture
  • Member

Posted 17 November 2012 - 10:23 AM

Thanks for you help Victor.

Unfortunately the first line (which was missing a ) )...

if(!empty($this->input->post())){


produces this error...

[b]Fatal error[/b]: Can't use method return value in write

I played around with different variations of the code but could not get it working at all. I appreciate the help.

victor

victor
  • profile picture
  • Member

Posted 17 November 2012 - 13:19 PM

try this: if($this->input->post('email')){...

carlosT

carlosT
  • profile picture
  • Member

Posted 22 November 2012 - 21:38 PM

Hi Victor,

I tried that, still no luck. Even though that seems like it should work. But $this->input->post(); is totally empty ? it doesnt print_r() anything.

Not sure where to go from here. I can get the primary key (id) using grocery's function and therefore get the email in the db but i can not get the email being posted to match it with.

victor

victor
  • profile picture
  • Member

Posted 23 November 2012 - 06:51 AM

Maybe it http://www.grocerycr...le-when-update/

carlosT

carlosT
  • profile picture
  • Member

Posted 24 November 2012 - 16:45 PM

I tried that and bizarrely it never calls the callback. Is there something special i have to do to ge it to actually do the callback ?

i have in my controller...

$crud->set_rules('email', 'Email','trim|required|valid_email|callback_email_check');


and the callback is...

  function email_check($str){   
$id = $this->uri->segment(4);
if(!empty($id) && is_numeric($id))
{
$email_old = $this->db->where("id", $id)->get('contacts')->row()->email;
if($str==$email_old)
{
return true;
}
else
{
$query = $this->db->get_where('contacts', array('email' => $str));
if($query->$query->num_rows() > 0)
{
$this->form_validation->set_message('email_check', 'The email already exists');
return false;
}
else
{
return true;
}
}
}
else
{
$query = $this->db->get_where("contacts", array('email' => $str));
if($query->$query->num_rows() > 0)
{
$this->form_validation->set_message('email_check', 'The email already exists');
return false;
}
else
{
return true;
}
}
}


The other things work like required and valid_email. This is driving me insane! I have even made a simple callback that sends me an email but it never sends anything.

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 27 November 2012 - 07:45 AM

[member='CarlosT'] I have an answer in stackoverflow at: http://stackoverflow...ion-or-callback by extending grocery CRUD. Probably this will be included to newer versions too. Hope it helps.

Rafael Alves

Rafael Alves
  • profile picture
  • Member

Posted 14 January 2013 - 20:20 PM

[quote name='web-johnny' timestamp='1354002307' post='4459']
[member='CarlosT'] I have an answer in stackoverflow at: http://stackoverflow...ion-or-callback by extending grocery CRUD. Probably this will be included to newer versions too. Hope it helps.
[/quote]

Hi Web-johny,

In this solution, how i do when have two tables?

$crud->set_rules("ip", "IP", "xss_clean|trim|is_unique[tab1.ip]|is_unique[tab2.ip]")