⚠ 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 output error message when using callback_before_insert



amityweb

amityweb
  • profile picture
  • Member

Posted 23 January 2012 - 15:59 PM

I am using callback_before_insert() to run some validation... if it fails then I want to show the error, and leave all the fileds in the form so they can correct it of course.

The problem is when success is false in the json_encode, I get errors, but it works OK when success is true.

So for example, this works OK, BUT the form fields are cleared (I assume because its success is true):


if($error)
{
echo "<textarea>".json_encode(array('success' => true , 'success_message' => 'This participant already exists'))."</textarea>";
$crud->set_echo_and_die();
}


If I use the following code (and use either success_message or error_message), I get a pop up message that says "An error occured on Saving"


if($error)
{
echo "<textarea>".json_encode(array('success' => false , 'error_message' => 'This participant already exists'))."</textarea>";
$crud->set_echo_and_die();
}


If I use the following code (to remove textarea), it just hangs (the animated loading image keeps going, no output)


if($error)
{
echo .json_encode(array('success' => false , 'error_message' => 'This participant already exists'));
$crud->set_echo_and_die();
}



So please could you tell me how do you output an error message to the screen when using callback_before_insert or callback_before_update?

Thanks

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 24 January 2012 - 20:06 PM

[quote name='amityweb' timestamp='1327334374' post='328']
I am using callback_before_insert() to run some validation... if it fails then I want to show the error, and leave all the fileds in the form so they can correct it of course.

The problem is when success is false in the json_encode, I get errors, but it works OK when success is true.

So for example, this works OK, BUT the form fields are cleared (I assume because its success is true):


if($error)
{
echo "<textarea>".json_encode(array('success' => true , 'success_message' => 'This participant already exists'))."</textarea>";
$crud->set_echo_and_die();
}


If I use the following code (and use either success_message or error_message), I get a pop up message that says "An error occured on Saving"


if($error)
{
echo "<textarea>".json_encode(array('success' => false , 'error_message' => 'This participant already exists'))."</textarea>";
$crud->set_echo_and_die();
}


If I use the following code (to remove textarea), it just hangs (the animated loading image keeps going, no output)


if($error)
{
echo .json_encode(array('success' => false , 'error_message' => 'This participant already exists'));
$crud->set_echo_and_die();
}



So please could you tell me how do you output an error message to the screen when using callback_before_insert or callback_before_update?

Thanks
[/quote]

I know what you mean but for now you are not able to do this. The only thing that you can do is to return false

if($error)
{
return false;
}

to your function and get the default error to the the CRUD. I know this is not the right way to do it, but it's the only way without hacking the code of grocery CRUD.
I hope it helps.

amityweb

amityweb
  • profile picture
  • Member

Posted 24 January 2012 - 22:14 PM

Thanks for letting me know. So in that case...

Is there an alternate way to validate fields? I have not used custom validation

Or could you point me in the direction on how to amend the grocerycrud core code to add the error message?

It's kind of important to tell the user what the error is!

For info, I am checking that three fields already exist or not (first name, last name, date of birth) to check if a person has already been added to the database, we do not want duplicates.

Thanks

Laurence

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 25 January 2012 - 06:01 AM

You can easily do this with the set_rules that you can also add custom callbacks. I also have an example of how to check unique data at : http://www.grocerycr...indpost__p__262 but you have to download the version 1.1.8 (just released :)) to do that as I had a mini BUG fixed for the set_rules with callback. If you don't want to update you can just update the three lines code that I mention to the post and have as many custom errors as you want.
The first idea of the error return false was that when there is a real error on database for example, the user don't care about for example: "MySQL Error: #1052 - Column 'id' in field list is ambiguous". Though to a developer that debugs its really important that's why I try to fix that.

amityweb

amityweb
  • profile picture
  • Member

Posted 25 January 2012 - 17:31 PM

I have been looking into this now. It looks promising. The only issue I have is how to pass multiple fields to the callback?

So I need to check 3 fields do not match 3 fields of another entry. From the examples I can only see how to pass one field.

When I add my rule though I can set a custom error message so looks promising!

Thanks

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 25 January 2012 - 18:56 PM

[quote name='amityweb' timestamp='1327512678' post='355']
I have been looking into this now. It looks promising. The only issue I have is how to pass multiple fields to the callback?

So I need to check 3 fields do not match 3 fields of another entry. From the examples I can only see how to pass one field.

When I add my rule though I can set a custom error message so looks promising!

Thanks
[/quote]

You can use set_rules 3 times for the 3 fields. Or else you can simply do it with the $_POST at once. The good part if you do it with the 3 seperate set_rules for each field you will also be able to have an error to your field also (a red border) like the image [attachment=23:2012-01-25_185443.png] , so that's better right?

amityweb

amityweb
  • profile picture
  • Member

Posted 26 January 2012 - 09:56 AM

Thanks, but I am still a bit confused sorry, hopefully this is the last question!

I need to check the 3 fields in the SAME function, not 3 separate times. I have to check that a form submission first name, last name and date of birth do not already exist (so a duplicate person). So having 3 set_rules looks like it will check each field individually, but not be able to check all 3 fields at the same time. Basically my function will query the database against the entered firstname, lastname and date of birth and check if the submitted form data is the same. My logic needs to be:




$duplicate_user = // SQL CHECK TO SEE IF $firstname, $lastname AND $dateofbirth ALl EXIST FOR A RECORD //
if ($duplicate_user == true)
{
$this->form_validation->set_message('participant_unique', 'A user with this firstname, lastname and date of birth already exists');
return FALSE;
}


So it looks as though I cannot use set_rules and pass an individual field to it.

So if I can use $_POST then that should work, but how do I pass that in the set_rules()? I will try now in case you dont reply in time.

Thanks

amityweb

amityweb
  • profile picture
  • Member

Posted 26 January 2012 - 10:23 AM

Ok I got it... I forget its just using Codeigniter functions and so found it on another forum... you can use the following in the function to get the other post values:

$participant_lastname = $this->input->post('participant_lastname');


So




function participant_unique($participant_firstname)
{
$participant_lastname = $this->input->post('participant_lastname');
$participants = // My SQL Database Check to check against several fields

if(count($participants) > 0)
{
$message = 'A participant with this firstname and lastname already exists';
$this->form_validation->set_message('participant_unique', $message);
return FALSE;
}
else
{
return TRUE;
}
}

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 26 January 2012 - 19:01 PM

[quote name='amityweb' timestamp='1327573415' post='363']
Ok I got it... I forget its just using Codeigniter functions and so found it on another forum... you can use the following in the function to get the other post values:

$participant_lastname = $this->input->post('participant_lastname');


So




function participant_unique($participant_firstname)
{
$participant_lastname = $this->input->post('participant_lastname');
$participants = // My SQL Database Check to check against several fields

if(count($participants) > 0)
{
$message = 'A participant with this firstname and lastname already exists';
$this->form_validation->set_message('participant_unique', $message);
return FALSE;
}
else
{
return TRUE;
}
}

[/quote]

Sorry for the misunderstanding. I thought that you wanted for 3 seperated fields. That's good that you find the answer ;)

neoblack

neoblack
  • profile picture
  • Member

Posted 10 May 2016 - 07:30 AM

hi,

why theme flexigrid show messege error when return false but theme bootstrap does not show messege error ? 

 

function _before_delete_callback($primary_key)
{
$this->load->model('video_m', 'video');
$this->load->model('relation_videos_display_m', 'revideos');
 
$item_video = $this->revideos->get_many_by($this->classname.'_id', $primary_key);
if ($item_video)
{
return FALSE;
 
return true;
}