⚠ 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 function checking in edit page



manojdamkondwar
  • profile picture
  • Member

Posted 09 April 2014 - 16:18 PM

Hi,

I have question,

 

I am trying to add unique value in my table filed it's working with grocery crud 'is_unique' function, but problem facing with edit page.

When I am trying to edit any value it's checking and getting error must need unique value if value is in table only one also..

plz any one can help me..


Amit Shah
  • profile picture
  • Member

Posted 09 April 2014 - 20:13 PM

Yes this is a common programing paradigm u need to understand my brother..

to manage the same u need to set a custom callback to the rule..

 

here is the sample you can look into ..

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

......and the callback function below


function email_check($email) {
		$primary_key = $this->uri->segment(5);      // this is what u need to adjust according to yr url pattern.. 
                                                                                    //mine is deep so i set it 5. u need to do it 4 yr own needs
		$rows = $this->cModel->getAllFor('retailer_acct', 'email', $email);
		if(!empty($primary_key) && is_numeric($primary_key))
		{
			if(count($rows)>0) {
				if($rows[0]['id'] != $primary_key) {
					$this->form_validation->set_message('email_check', 'The email - ' . $email . ' already is assigned to some other user');
					return false;
				}
			}
			return true;
		} else {
			if(count($rows)>0) {
				$this->form_validation->set_message('email_check', 'The email - ' . $email . ' already is assigned to some other user');
				return false;
			} else {
				return true;
			}
		}
	}

xheradon
  • profile picture
  • Member

Posted 01 March 2017 - 14:03 PM

 

Yes this is a common programing paradigm u need to understand my brother..

to manage the same u need to set a custom callback to the rule..

 

here is the sample you can look into ..

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

......and the callback function below

function email_check($email) {
		$primary_key = $this->uri->segment(5);      // this is what u need to adjust according to yr url pattern.. 
                                                                                    //mine is deep so i set it 5. u need to do it 4 yr own needs
		$rows = $this->cModel->getAllFor('retailer_acct', 'email', $email);
		if(!empty($primary_key) && is_numeric($primary_key))
		{
			if(count($rows)>0) {
				if($rows[0]['id'] != $primary_key) {
					$this->form_validation->set_message('email_check', 'The email - ' . $email . ' already is assigned to some other user');
					return false;
				}
			}
			return true;
		} else {
			if(count($rows)>0) {
				$this->form_validation->set_message('email_check', 'The email - ' . $email . ' already is assigned to some other user');
				return false;
			} else {
				return true;
			}
		}
	}

 

I have

 

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

 

and 

public function email_check($email) {
        $primary_key = $this->uri->segment(4);     
        dump($email);
        dump($primary_key);
        $this->form_validation->set_message('email_check', 'The email - ' . $email . ' already is assigned to some other user');
        $rows = $this->cModel->getAllFor('retailer_acct', 'email', $email);
        if(!empty($primary_key) && is_numeric($primary_key))
        {
            if(count($rows)>0) {
                if($rows[0]['id'] != $primary_key) {
                    $this->form_validation->set_message('email_check', 'The email - ' . $email . ' already is assigned to some other user');
                    return false;
                }
            }
            return true;
        } else {
            if(count($rows)>0) {
                $this->form_validation->set_message('email_check', 'The email - ' . $email . ' already is assigned to some other user');
                return false;
            } else {
                return true;
            }
        }
    }

But alwayis returning FIXME('form_validation_email_check')

 

Can u help me? thanks