Jump to content


[SOLVED] Set rules is unique doesn't work


  • Please log in to reply
14 replies to this topic

#1 clare

clare

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 04 January 2012 - 12:16 PM

Hi,

Is there any way to have different validation rules for insert and update? For example, I want the usernames to be unique, but if i do
$this->grocery_crud->set_rules('username', 'Username','trim|required|xss_clean|is_unique[Users.username]');
it doesn't allow me to update the record because it thinks the username is being duplicated...

Thanks

#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,099 posts
  • LocationLondon

Posted 05 January 2012 - 11:23 PM

Hi,

Is there any way to have different validation rules for insert and update? For example, I want the usernames to be unique, but if i do

$this->grocery_crud->set_rules('username', 'Username','trim|required|xss_clean|is_unique[Users.username]');
it doesn't allow me to update the record because it thinks the username is being duplicated...

Thanks


The only way you can do it now without hacking the core is this:
function your_function($operation = null)
{
....
$this->grocery_crud->....
if( $operation == 'insert_validation' || $operation == 'insert')
{
  $this->grocery_crud->set_rules('username', 'Username','trim|required|xss_clean|is_unique[Users.username]');
}
else
{
$this->grocery_crud->set_rules('username', 'Username','trim|required|xss_clean');
}
$this->grocery_crud->....
$output = $this->grocery_crud->render();

....
}

I know this is not the best solution, but it will work for sure .

The best thing for me to do its to use custom callback for the set_rule. You can see more at: http://codeigniter.c....html#callbacks
Posted Image

#3 clare

clare

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 09 January 2012 - 08:49 PM

Hi thanks for your reply,

Your solution works if the username is only ever changed on insert, but the username can still be changed to another already existing username on update.... is there a way of making the username field un-editable on update?

Thanks

#4 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,099 posts
  • LocationLondon

Posted 11 January 2012 - 02:38 AM

This solution works for grocery CRUD version 1.1.8 or later
You can use the callback function, so the solution of your problem is:

function your_function($operation = null)
{
....
$this->grocery_crud->....
  $this->grocery_crud->set_rules('username', 'Username','trim|required|xss_clean|callback_username_check');
$this->grocery_crud->....
$output = $this->grocery_crud->render();
....
}
public function username_check($str)
{
  $id = $this->uri->segment(4);
  if(!empty($id) && is_numeric($id))
  {
   $username_old = $this->db->where("id",$id)->get('users')->row()->username;
   $this->db->where("username !=",$username_old);
  }
  
  $num_row = $this->db->where('username',$str)->get('users')->num_rows();
  if ($num_row >= 1)
  {
   $this->form_validation->set_message('username_check', 'The username already exists');
   return FALSE;
  }
  else
  {
   return TRUE;
  }
}

Try it and inform us, just for the other users to know about similar issues.
Posted Image

#5 clare

clare

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 11 January 2012 - 01:49 PM

Yep the callback function works well :)

I needed to add more lines into the library, as the version I have doesn't seem to be up-to-date (it was just some extra stuff from the line surrounding the edits in those 2 commits).

I've tried adding a callback function for other fields and it seems to work perfectly :)

Thanks very much!
Clare

#6 arif_avi

arif_avi

    Member

  • Members
  • PipPip
  • 12 posts

Posted 12 January 2012 - 07:04 AM

This will check both before insert and during updates...
@web-johnny Thanks for the last post... i just modified it for my use...

/***** rule + code *****/
$crud->set_rules('batchName','Batch Name','trim|required|xss_clean|callback_checkUniqueBatchName');
function checkUniqueBatchName($str){
	    //is_unique['.TBL_BATCH.'.batchName]
	    $id = $this->uri->segment(4);
	    $old_name = "";
	    $result = null;
	 
	    if(!empty($id) && is_numeric($id))
	    {
		    $this->db->where("batchId", $id);		 
		    $result = $this->db->get(TBL_BATCH);
		    if($result->num_rows() > 0)
		    {
			    foreach($result->result() as $row)
			    {
				    $old_name = $row->batchName;
			    }
			 
			    $this->db->where("batchName !=", $old_name);
			    $this->db->where("batchName", $str);
			    $num_rows = $this->db->get(TBL_BATCH)->num_rows();
			    if($num_rows > 0)
			    {
				    $this->form_validation->set_message("checkUniqueBatchName","The %s already Exist. Please try a different batch name.");
				    return false;
			    }
		    }
	    }
	    else
	    {
		    $this->db->where("batchName", $str);
		    $num_rows = $this->db->get(TBL_BATCH)->num_rows();
		    if($num_rows > 0)
		    {
			    $this->form_validation->set_message("checkUniqueBatchName","The %s already Exist. Please try a different batch name.");
			    return false;
		    }
	    } 
    }


#7 Lucas

Lucas

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts
  • LocationMedellin - Colombia

Posted 11 March 2012 - 05:07 PM

ok but only update option ?
function proyectos_participantes_consulta() {
	    $this->load->library('session');
	    if (!$this->session->userdata('proyecto'))
		    $this->session->set_userdata('proyecto', $this->input->post('proyecto'));
	    $this->load->helper(array('form', 'url'));
	    $this->load->library('form_validation');
	    $crud = new grocery_CRUD();
	    $crud = new grocery_CRUD();
	    $crud->set_table('fs_usuario_proyecto');
	    $crud->set_theme('flexigrid');
	   // $crud->set_rules('fs_usuario_id', 'Username', 'callback_username_check');
	    $crud->where('fs_proyecto_id', $this->session->userdata('proyecto'));
	    $crud->set_subject('Participantes por Proyecto');
	    $crud->unset_columns('id', 'usuario_proyecto_nombre_heredado');
	    $crud->set_relation('fs_proyecto_id', 'fs_proyecto', 'proyecto_nombre');
	    $crud->set_relation('fs_rol_id', 'fs_rol', 'rol_nombre');
	    $crud->set_relation('fs_usuario_id', 'fs_usuario', '{usuario_nombre}  {usuario_apellido} ');
	    $crud->display_as('fs_proyecto_id', 'Proyecto');
	    $crud->display_as('fs_rol_id', 'Rol en el Proyecto');
	    $crud->display_as('fs_usuario_id', 'Usuario');
	    $crud->callback_after_insert(array($this, 'nombre_participantes'));
	    $crud->callback_after_update(array($this, 'nombre_participantes'));
	    $crud->callback_after_update(array($this, 'username_check'));
	    $this->grocery_crud->callback_before_insert(array($this, 'autoProyecto'));
	    $crud->change_field_type('fs_proyecto_id', 'hidden', $this->session->userdata('proyecto'));
	    $crud->fields('fs_proyecto_id', 'fs_rol_id', 'fs_usuario_id');
	    $crud->edit_fields('fs_rol_id');
	    $output = $crud->render();
	    $this->_example_output($output);
    }
// Verifica que el usuario no se encuentre ya registrado en el proyecto
    function username_check($str,$hey=null) {
	    if ($this->proyecto_model->usuarioProyecto($str['fs_usuario_id'], $this->session->userdata('proyecto')) == true) {
		    $this->form_validation->set_message('Usuario', 'El Usuario ya se encuentra registrado en el Proyecto"');
		    return FALSE;
	    } else {
		    return TRUE;
	    }
    }


#8 rikoy

rikoy

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 29 June 2012 - 02:56 AM

thanks jhonny, can do use one function to handle this many same process?

#9 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,099 posts
  • LocationLondon

Posted 01 July 2012 - 01:09 AM

Well I want for the future to add something to this:


$crud->unique_fields('field1','field2','field3',...);

Update: Guys whoever is intrested to find an external library for the functionality $crud->unique_fields you can check my answer at stackoverflow at: http://stackoverflow...ion-or-callback


Posted Image

#10 rteranm

rteranm

    Member

  • Members
  • PipPip
  • 10 posts

Posted 18 July 2012 - 10:37 PM

This solution works for grocery CRUD version 1.1.8 or later
You can use the callback function, so the solution of your problem is:

function your_function($operation = null)
{
....
$this->grocery_crud->....
  $this->grocery_crud->set_rules('username', 'Username','trim|required|xss_clean|callback_username_check');
$this->grocery_crud->....
$output = $this->grocery_crud->render();
....
}
public function username_check($str)
{
  $id = $this->uri->segment(4);
  if(!empty($id) && is_numeric($id))
  {
   $username_old = $this->db->where("id",$id)->get('users')->row()->username;
   $this->db->where("username !=",$username_old);
  }
  
  $num_row = $this->db->where('username',$str)->get('users')->num_rows();
  if ($num_row >= 1)
  {
   $this->form_validation->set_message('username_check', 'The username already exists');
   return FALSE;
  }
  else
  {
   return TRUE;
  }
}

Try it and inform us, just for the other users to know about similar issues.



great code, but when you want upgrade "The username already exists"

regards

#11 Nico

Nico

    Member

  • Members
  • PipPip
  • 11 posts
  • LocationQuebec, Canada

Posted 03 August 2012 - 03:11 AM

But there has to be a way to get the primary key on edition for the rule(which you don't have on addition...so, it would help to know what is the current operation) which could help to find, if the username is not unique, if at least it is the user's one. if so, the update runs, else, an error appears.


It could prevent to have to disable the username field during edition...

((I have that issue and instead of starting another thread, I use the one that has an issue slightly different than me...I don't know if it's a problem...))

I need to have access to the username but keep the rule that verify if it is unique even if i have to make a callback...



[UPDATE] Oops...i didn't see the SOLVED...sorry, should I start a new thread then????

Edited by Nico, 03 August 2012 - 03:13 AM.

-- Nico --

#12 casachit

casachit

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts

Posted 09 August 2012 - 10:14 AM

This solution works for grocery CRUD version 1.1.8 or later
You can use the callback function, so the solution of your problem is:

function your_function($operation = null)
{
....
$this->grocery_crud->....
  $this->grocery_crud->set_rules('username', 'Username','trim|required|xss_clean|callback_username_check');
$this->grocery_crud->....
$output = $this->grocery_crud->render();
....
}
public function username_check($str)
{
  $id = $this->uri->segment(4);
  if(!empty($id) && is_numeric($id))
  {
   $username_old = $this->db->where("id",$id)->get('users')->row()->username;
   $this->db->where("username !=",$username_old);
  }
  
  $num_row = $this->db->where('username',$str)->get('users')->num_rows();
  if ($num_row >= 1)
  {
   $this->form_validation->set_message('username_check', 'The username already exists');
   return FALSE;
  }
  else
  {
   return TRUE;
  }
}

Try it and inform us, just for the other users to know about similar issues.


I dunno why this code is not working for me... It keeps throwing error when updating similar to is_unique error...

#13 arif rh

arif rh

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 12 September 2012 - 09:33 AM

I have read how to make the same rule for is_unique when editing and adding entries, but it doesn't work when in grocery CRUD, just work when adding data, but when update data it always says that "The %s field must contain a unique value.".

Here's the code, how can I modify it to make it work with grocery CRUD? Any help, please.

public function is_unique($str, $field)
{
  list($table, $field)=explode('.', $field);
  $q = $this->CI->db->query("SHOW KEYS FROM $table WHERE Key_name = 'PRIMARY'")->row();
  $primary_key = $q->Column_name;

  if($this->CI->input->post($primary_key) > 0):
   $query = $this->CI->db->limit(1)->get_where($table, array($field => $str,$primary_key.' !='=>$this->CI->input->post($primary_key)));
  else:
   $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
  endif;

  return $query->num_rows() === 0;
	}

Edited :

Those lines has worked fine, I've made mistake when post data. Thanks.

#14 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,099 posts
  • LocationLondon

Posted 26 November 2012 - 12:21 PM

Guys whoever is intrested to find an external library for the functionality $crud->unique_fields you can check my answer at stackoverflow at: http://stackoverflow...ion-or-callback
Posted Image

#15 edramirez

edramirez

    Advanced Member

  • Members
  • PipPipPip
  • 63 posts
  • LocationMetro Manila

Posted 10 November 2013 - 03:27 PM

In ancient times, we didn't create tables with a separate id field like this:

 

id integer autoincrement

username  varchar(10)

password varchar(40)

with id as primary key

 

The way we did this was like this was to eliminate  the autoincrement field called id and assign the primary key to the username. In this way, we allowed the database to handle the validation and process the add or edit request if and only if the constraint requirements were met. I myself still do it this way, following the ancient normal form rules. By doing so, one need not enforce uniqueness in a field that isn't the primary key anyway.

 

Sometimes there is great wisdom in returning to the ancient ways and follow the path of the ancient ones. hehehe

 

Regards,

 

Ed Ramirez

 

P.S. Don't forget to click the Like button...  :rolleyes:






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users