Jump to content


is_unique validation problem on edit

is_unique validation set_rules

  • Please log in to reply
8 replies to this topic

#1 carlosT

carlosT

    Member

  • Members
  • PipPip
  • 17 posts

Posted 15 November 2012 - 08: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 ?

#2 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 15 November 2012 - 10: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();


Edited by victor, 15 November 2012 - 10:30 PM.


#3 carlosT

carlosT

    Member

  • Members
  • PipPip
  • 17 posts

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...

Fatal error: 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.

#4 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 17 November 2012 - 01:19 PM

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

#5 carlosT

carlosT

    Member

  • Members
  • PipPip
  • 17 posts

Posted 22 November 2012 - 09: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.

#6 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 23 November 2012 - 06:51 AM

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

#7 carlosT

carlosT

    Member

  • Members
  • PipPip
  • 17 posts

Posted 24 November 2012 - 04: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.

#8 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,101 posts
  • LocationLondon

Posted 27 November 2012 - 07:45 AM

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.
Posted Image

#9 Rafael Alves

Rafael Alves

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationCuritiba, Brazil

Posted 14 January 2013 - 08:20 PM

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.


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]")





Also tagged with one or more of these keywords: is_unique, validation, set_rules

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users