⚠ 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

Error management in callback_update



Raúl
  • profile picture
  • Member

Posted 15 July 2015 - 17:09 PM

Hi

I would need to handle errors after updating an entry with callback update (and I will need also for new in callback_insert)

 

Basically, I want to create a new group for ION_AUTH using their API so instead of simply adding an entry to the table, I would like to use the callback to use the ion_auth->create_group method.  This method returns FALSE in case an error occurs so I would need to handle that feedback from the method and show a message

 

Any idea?

$this->grocery_crud->callback_update(array($this,'grupo_change'));
.....
  	  	
function grupo_change($post_array, $primary_key)
    {
    	$updated = $this->ion_auth->update_group($primary_key,$post_array['name'],$post_array['description']);
  
    	if  (!$updated)
    	{	
    	
    		
                     
    	}
    
    	
    	
    	  
  	} 
  	

Paul Savostin
  • profile picture
  • Member

Posted 15 July 2015 - 21:38 PM

Hi! So find out why that function return FALSE and write custom validation rule for it reason ;)


Raúl
  • profile picture
  • Member

Posted 15 July 2015 - 22:30 PM

He he, thanks but not my style ;)
I already added the validation for name in use , that I guess it is the source of 90% of the cases. However, I wouldn't like to show the default success message in case something fails (db, connection?). I would expect that, if we have an entry point to add logic (callback), we should have the chance to send also custom messages

Paul Savostin
  • profile picture
  • Member

Posted 15 July 2015 - 23:56 PM

So what type of message u want to show when fails db connection? lol

That callback is for doing some action after all checks, say in your own way.

And I wonder that u want send custom messages in this case


Raúl
  • profile picture
  • Member

Posted 17 July 2015 - 07:41 AM

Well, db was perhaps not the best example  :D

Yes, the idea was simply that: I check whatever I know that I can check (no duplicate name, value higher than limit) but in case something unexpected fails, need to communicate to users (that is, when a method returns a fail for instance).

 

Finally, I found what I needed and I have to confess it was really simple (newbie stuff I am afraid): I simply had to include a return in my callback to indicate whether it was right or wrong. So, in this case, I am returning the output of update_group method as result of my call back 

 

Thanks for the support


Paul Savostin
  • profile picture
  • Member

Posted 17 July 2015 - 10:25 AM

Yes if u implement insert/update with your own callback method then u need to return true or false.

but I thought that u need to customize this error message !? (by default now "An error has been occured at the insert/update.")

As I know its impossible yet. Well I am glad that u find the solution. Have a nice day!

 


Json Sean
  • profile picture
  • Member

Posted 01 November 2017 - 05:35 AM

/topic/2109-error-messages/#entry15705

 

 


Here is the way to custom error message when editing :

 

1、modify Grocery_CRUD.php

protected function db_update($state_info)
{
...
if($this->callback_before_update !== null)
{
$callback_return = call_user_func($this->callback_before_update, $post_data, $primary_key);

if(!empty($callback_return) && is_array($callback_return))
{
$post_data = $callback_return;
}
elseif($callback_return === false)
{
return false;
+ }elseif( is_string($callback_return))
+ {
+ return array('status' => false,'message' => $callback_return);
+ }

}

...

}protected function update_layout($update_result = false, $state_info = null)
{
@ob_end_clean();
if($update_result === false)
{
echo json_encode(array('success' => $update_result));
+ }elseif(isset($update_result['status'])){
+ //TODO:custom error
+ $error_message = '<p>'.$update_result['message']. '</p>';
+ echo json_encode(array(
+ 'success' => $update_result['status'] ,
+ 'error_message' => $error_message,
+ ));
+ }
else
{

...

}

2、edit.js

...

$('#crudForm').ajaxSubmit({
dataType: 'text',
cache: 'false',
beforeSend: function(){
$("#FormLoading").show();
},
success: function(result) {
data = $.parseJSON(result);
if (data.success) {

if(save_and_close)
{
if ($('#save-and-go-back-button').closest('.ui-dialog').length === 0) {
window.location = data.success_list_url+'?type=edit';
//form_success_message(data.success_list_url);
} else {
$(".ui-dialog-content").dialog("close");
success_message(data.success_message);
}

return true;
}

form_success_message(data.success_message);
+ }else if (!data.success & data.error_message != null){
+ //TODO: custom error message
+ form_error_message(data.error_message);
+ } else {
form_error_message(message_update_error);
}
},
error: function(){
form_error_message( message_update_error );
}
});

...

 

 

3、in you callback function, just return the string of your error message:

function before_xxx_update_callback($post_array, $primary_key) {
if( error ){
$message = 'error message';
return $message;
}

return $post_array;
}

To make it extendibility , you may define Cutom_Grocery_CRUD.php extending Grocery_CRUD.php , then modify Cutom_Grocery_CRUD.php.