⚠ 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

Compare two fields



kelli
  • profile picture
  • Member

Posted 06 December 2013 - 11:27 AM

Hello,

I  have an add record form and i want to compare  password and confirm password. How can i do this in grocery crud? Can you give me an example?

 


Amit Shah
  • profile picture
  • Member

Posted 06 December 2013 - 12:57 PM

Hi there,

 

Here is the following u can do.. adjust it according to your needs.. i have done it my way...

function users($rid=FALSE) {
	if(!is_numeric($rid)) {
		show_error("Cannot proceed - Required parameters are missing or invalid");
	}
	$crud = new Grocery_CRUD();
	$crud->set_table('retailer_acct');
	
	$retailer = $this->cModel->getByField('retailers', 'rid', $rid);
	if(count($retailer) <= 0) {
		show_error("Cannot proceed - No such retailer was found");
	}
	
	$crud->set_subject('Users associated to - ' . $retailer['name']);
	
	$crud->columns('firstname', 'lastname', 'email');
	$crud->add_fields('firstname', 'lastname', 'email', 'password', 'confirm_password', 'user_level');
	$crud->edit_fields('firstname', 'lastname', 'email', 'password', 'confirm_password');

	$crud->unset_read();
	$crud->unset_edit();
	$crud->unset_delete();
	
	$crud->unset_jquery();
	
	$crud->display_as('firstname', 'First Name');
	$crud->display_as('lastname', 'Last Name');
	
	//$crud->unset_back_to_list();
	$crud->callback_before_insert('unset_password');
	$crud->callback_after_insert('set_user_levels');
	
	$crud->set_rules('firstname', 'First Name', 'required');
	$crud->set_rules('email', 'Email', 'required|email|is_unique[users.email]');
	$crud->set_rules('confirm_password', 'Confirm Password', 'required');
	$crud->set_rules('password', 'Password', 'required|matches[confirm_password]');
	$crud->change_field_type('user_level', 'hidden', 'Manager');
	
	$crud->where('id in (select raid from ra_access where rid = ' . $rid . ')');
	
	//Render the stuff
	$output = $crud->render();
	$data = array();
	$data['retailer'] = $retailer;
	$output->data = $data;
	$this->load->view('retailer_crud',$output);
}

//Once you done with the confirm password and all.. u need to unset it before u alllow the GC to make entry into the system...
function unset_password($POST_ARRAY) {
	//Unset the confirm password
	unset($POST_ARRAY['confirm_password']);
	
	//Generated encrypted password
	$pass = $POST_ARRAY['pssword'];
	$ci =& get_instance();
	$salt = $ci->config->item('auth_salt', 'ion_auth');
	$POST_ARRAY['pssword'] = sha1($salt . $pass);
	
	//Return the array with encrypted password
	return $POST_ARRAY;
}

Mind it - here the code is as per my requirements ... i am sharing u a piece of actual project work... alter and use it the way you want..!!

 

Happy GCing :)


kelli
  • profile picture
  • Member

Posted 06 December 2013 - 13:28 PM



Hi there,

 

Here is the following u can do.. adjust it according to your needs.. i have done it my way...

function users($rid=FALSE) {
	if(!is_numeric($rid)) {
		show_error("Cannot proceed - Required parameters are missing or invalid");
	}
	$crud = new Grocery_CRUD();
	$crud->set_table('retailer_acct');
	
	$retailer = $this->cModel->getByField('retailers', 'rid', $rid);
	if(count($retailer) <= 0) {
		show_error("Cannot proceed - No such retailer was found");
	}
	
	$crud->set_subject('Users associated to - ' . $retailer['name']);
	
	$crud->columns('firstname', 'lastname', 'email');
	$crud->add_fields('firstname', 'lastname', 'email', 'password', 'confirm_password', 'user_level');
	$crud->edit_fields('firstname', 'lastname', 'email', 'password', 'confirm_password');

	$crud->unset_read();
	$crud->unset_edit();
	$crud->unset_delete();
	
	$crud->unset_jquery();
	
	$crud->display_as('firstname', 'First Name');
	$crud->display_as('lastname', 'Last Name');
	
	//$crud->unset_back_to_list();
	$crud->callback_before_insert('unset_password');
	$crud->callback_after_insert('set_user_levels');
	
	$crud->set_rules('firstname', 'First Name', 'required');
	$crud->set_rules('email', 'Email', 'required|email|is_unique[users.email]');
	$crud->set_rules('confirm_password', 'Confirm Password', 'required');
	$crud->set_rules('password', 'Password', 'required|matches[confirm_password]');
	$crud->change_field_type('user_level', 'hidden', 'Manager');
	
	$crud->where('id in (select raid from ra_access where rid = ' . $rid . ')');
	
	//Render the stuff
	$output = $crud->render();
	$data = array();
	$data['retailer'] = $retailer;
	$output->data = $data;
	$this->load->view('retailer_crud',$output);
}

//Once you done with the confirm password and all.. u need to unset it before u alllow the GC to make entry into the system...
function unset_password($POST_ARRAY) {
	//Unset the confirm password
	unset($POST_ARRAY['confirm_password']);
	
	//Generated encrypted password
	$pass = $POST_ARRAY['pssword'];
	$ci =& get_instance();
	$salt = $ci->config->item('auth_salt', 'ion_auth');
	$POST_ARRAY['pssword'] = sha1($salt . $pass);
	
	//Return the array with encrypted password
	return $POST_ARRAY;
}

Mind it - here the code is as per my requirements ... i am sharing u a piece of actual project work... alter and use it the way you want..!!

 

Happy GCing :)

 

Thank you very much! I found another way to do it and i already fixed it! I made two functions  to my controller after index and now it's ok. That's what i did:

 

function management()
{
        $crud = new grocery_CRUD();
        $crud->set_table('users');
        $crud->columns('enable','registered', 'username', 'email');
        $crud->set_subject('User');
        $crud->fields('username', 'password','passconf', 'email');
        $crud->field_type('enable', 'true_false');
        $crud->field_type('email', 'email');
        $crud->set_rules('password', 'Password', 'required|matches[passconf]');
        $crud->set_rules('passconf', 'Password Confirmation', 'required');
        $crud->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
        $crud->callback_before_insert(array($this,'before_user_insert_callback'));
        
        
        $data = $crud->render();
        $this->load->view('templates/header', $data);
        $this->load->view('templates/navbar');
        $this->load->view('users/manage.php', $data);  
        $this->load->view('templates/footer');
       
}
 
       
    function before_user_insert_callback($post_array)
    {
        $this->load->model('user_model');
        unset($post_array['passconf']);
        $post_array['password'] = $this->user_model->hash($post_array['password']);
        $post_array['registered'] =time();
        $post_array['enabled'] =1;
         return $post_array;
    }