⚠ 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

callback_before_insert issue



amipax
  • profile picture
  • Member

Posted 19 November 2012 - 17:10 PM

hi, I have 2 callbacks , one used for unsetting a password verification field, the other encripts a password, but only the latest one execute, even if a change the callbacks order.


function display_grid(){

$this->load->library('form_validation');
$data['titulo'] = "Tabla de elementos";
$data['contenido'] = 'cms_elementos_grid';
$crud = new grocery_CRUD();
$crud->set_subject('usuario');
$crud->set_table('usuarios');
$crud ->columns('id', 'email', 'tipo', 'estado', 'creado_en');
$crud ->fields('email', 'clave', 'vclave','tipo', 'estado');

$crud->display_as('vclave', 'Verificar Clave');
$crud->change_field_type('clave', 'password');
$crud->change_field_type('vclave', 'password');
$crud->required_fields('email', 'clave','vclave', 'tipo');

$crud->set_rules('email', 'Correo Electronico', 'valid_email|callback_username_check'); <---- at updating throws email already exists message as declared on callback ..
$crud->set_rules('clave', 'Clave', 'required|trim|max_length[20]');
$crud->set_rules('vclave', 'Verificar Clave', 'required|trim|matches[clave]|max_length[20]');


$crud->callback_before_insert(array($this,'unset_vclave'));

$crud->callback_before_insert(array($this,'encriptar_clave')); <-----THIS ONE RUN

$output = $crud->render();
$data['tablecrud'] = $output;
$this->load->view('cms_plantilla',$data);

}


function username_check($str)
{

$num_row = $this->db->where('email', $str)->get('usuarios')->num_rows();

if($num_row > 0)
{
$this->form_validation->set_message('username_check', 'El email ya existe en el sistema');
return FALSE;
}
else
{
return TRUE;
}
}



function unset_vclave($post_array){

unset($post_array['vclave']);
return $post_array;
}

function encriptar_clave($post_array){

$post_array['clave'] = substr(do_hash($post_array['clave']),0,16);
//print_r($post_array);
return $post_array;
}

victor
  • profile picture
  • Member

Posted 19 November 2012 - 18:21 PM

Hi! I don't understand why you need two callback function ?

amipax
  • profile picture
  • Member

Posted 19 November 2012 - 18:40 PM

hi victor, I used two callback functions to have a proper name for every process in execution (ex. unset_password, encript_password) , but as you mention it, I could have used one and it actually works using one callback (problem solved), is there any reason why it doesn't work with 2 callbacks ?

Spasibo /thanks

victor
  • profile picture
  • Member

Posted 19 November 2012 - 18:47 PM

the answer is simple: it is possible to use only once this function :)

amipax
  • profile picture
  • Member

Posted 19 November 2012 - 18:56 PM

Ok, :D

amipax
  • profile picture
  • Member

Posted 19 November 2012 - 19:05 PM

I have a new problem at set_rules('email', ' callback_check_username') when trying to "update" status of a user , it throws a email already exist message, i tried to bypass it when editing using $crud->getState == edit but I couldn't make it work. i keep getting the message declared on the callback.

victor
  • profile picture
  • Member

Posted 19 November 2012 - 19:46 PM


if($crud->getState()=='edit')
{
if($this->input->post('email'))
{
$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]');
}

amipax
  • profile picture
  • Member

Posted 19 November 2012 - 21:32 PM

that works great!