⚠ 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

how to show custom error message after callback_before_insert



Ankit Limkar

Ankit Limkar
  • profile picture
  • Member

Posted 26 October 2015 - 08:28 AM

Hi there,

 

I have just started using GC and it's my learning curve.

 

I have written a callback which checks for record duplication by checking three columns simultaneously (Joining 3 WHEREs by 2 ANDs in a query).

 

 

function check_duplicate_class($post_array)
    {
        $this->load->model('class_model');
        $does_exist = $this->class_model->check_duplicate();
        if(!empty($does_exist))
        {
            $message = 'A class already exists';
            $this->form_validation->set_message('participant_unique', $message);
            return FALSE; 
        }
        else
        {
            return TRUE;
        }
    }

 

This works fine and prevents duplicate records.

but the problem is the message I have set is not shown. Instead javascript message "An error has occured on insert". What I am missing or what should I do in addition?


Paul Savostin

Paul Savostin
  • profile picture
  • Member

Posted 27 October 2015 - 01:07 AM

Hi! You have standart error message when something goes wrong and u return false.

 

Maybe u should try to use some sort validation for this situation where u could set custom error message!

 

Did u try using set_rules ?


Ankit Limkar

Ankit Limkar
  • profile picture
  • Member

Posted 27 October 2015 - 03:50 AM

Thanks Paul.

 

Nope. I haven't tried set_rules as I'm comparing three post values simultaneously against database values and that doesn't fit into set_rules scope.

 

I am using callback_before_insert & callback code I've posted already.

 

Further, I think there's something that can be done particularly at line $this->form_validation->set_message('participant_unique', $message);


titu

titu
  • profile picture
  • Member

Posted 27 October 2015 - 15:17 PM

Check this example from SO.

 

You can check multiple values with set_rules:

 

$this->form_validation->set_rules('second_field', 'Second Field', 'trim|required|is_natural_no_zero|callback_check_equal_less['.$this->input->post('first_field').']')

 

 

function check_equal_less($second_field,$first_field)
{
if ($second_field <= $first_field)
{
$this->form_validation->set_message('check_equal_less', 'The First &amp;/or Second fields have errors.');
return false;
}
else
{
return true;
}
}


Paul Savostin

Paul Savostin
  • profile picture
  • Member

Posted 27 October 2015 - 15:20 PM

Guys I dont understand it at all. In custom callback set_rules function u can access to all POST fields! Yes or no?

So why dont get  all fields that u need and do whatever validation u want with whatever custom msgs u want!

Correct me if I'm wrong


titu

titu
  • profile picture
  • Member

Posted 27 October 2015 - 15:35 PM

I never tried it but I believe you can.


Ankit Limkar

Ankit Limkar
  • profile picture
  • Member

Posted 28 October 2015 - 04:11 AM

Oh man...Yes I tried the way you showed & it worked.

 

Pardon my ignorance. :(

 

Thanks Paul and Titu for help. :)


Paul Savostin

Paul Savostin
  • profile picture
  • Member

Posted 28 October 2015 - 15:01 PM

No problem, you're welcome!


Json Sean

Json Sean
  • profile picture
  • Member

Posted 01 November 2017 - 05:33 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;
}elseifis_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',
beforeSendfunction(){
$("#FormLoading").show();
},
successfunction(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);
}
},
errorfunction(){
form_error_messagemessage_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.