⚠ 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 is not working in a dabase relation.



joshyro
  • profile picture
  • Member

Posted 22 November 2012 - 17:15 PM

Hi everyone!

I'm trying to validate a duplicate entry on my database, the example with duplicate username It works, but when I want use this for a field with a database relations, It doesn't show the message of error like duplicate username's validation example.

In this fuzzy example I want to know if exist a duplicate officeCode:



function employees_management()
{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('employees');
$crud->set_relation('officeCode','offices','city');
$crud->display_as('officeCode','Office City');

// I've tried with callback_before_insert and with rules.

// $crud->callback_before_insert(array($this,'officeCode_check'));
// $crud->set_rules('officeCode', 'Office City','officeCode_check');

$crud->set_subject('Employee');

$crud->required_fields('lastName');

$crud->set_field_upload('file_url','assets/uploads/files');

$output = $crud->render();
$this->_example_output($output);
}

function officeCode_check($str)
{

$num_row = //My query here;

if ($num_row >= 1)
{
$this->set_message('officeCode_check', 'Already updated with this Code!');
return FALSE;
}
else
{
return TRUE;
}
}



I'm something missing?

Regards.

saulimus
  • profile picture
  • Member

Posted 22 November 2012 - 20:42 PM

You can only modify the $post_array using callback_before_insert. So it's not very useful in this case...

You need the prefix 'callback_' before your actual function name in set_rules(), like this:
$crud->set_rules('officeCode', 'Office City','[b]callback_[/b]officeCode_check');
Then this should work:


function officeCode_check($str)
{
$num_row = //My query here;
if ($num_row >= 1)
{
$message = $this->lang->line('is_unique');
$message = sprintf($message, 'Office City');
$this->form_validation->set_message('officeCode_check', $message);
return FALSE;
}
else
return TRUE;
}


Also, you'll have to load the form_validation library before using it.

joshyro
  • profile picture
  • Member

Posted 22 November 2012 - 23:31 PM

[b] Hi @saulimus[/b]

Thanks for your reply, ( Yes I've forgotten callback_ prefix in this example ). When I apply this code It appears 'loading saving data...' and no more, I've attached an image. Could be an error with my query? [attachment=371:MyScreenshot1.png]

PS: In my scenario the user can have 3 codes but not duplicates codes, so I think validating the field is the best way.

joshyro
  • profile picture
  • Member

Posted 23 November 2012 - 01:06 AM

I got It, the problem was with my query. This is a post how we can debug in grocery crud. http://stackoverflow.com/questions/8201343/debugging-in-grocery-crud