Prevent Insert with custom message?

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
User avatar
gregsintx
Posts: 15
Joined: 2015-05-09 06:25

Prevent Insert with custom message?

Post by gregsintx » 2015-06-25 15:37

Hi folks

I am trying to figure out a way to prevent inserting data that would be considered a duplicate of existing data, and provide a clean message letting the user know that they have already entered such information.

I was looking at doing SQL in the hooks for tablename_before_insert to query the DB using the $data from the existing page, but then my only option is to return FALSE to prevent the insert, not give some nice message.

IE I am trying to prevent this from happening:
2015-06-25_0932.png
2015-06-25_0932.png (60.65 KiB) Viewed 14358 times

udayvatturi
AppGini Super Hero
AppGini Super Hero
Posts: 85
Joined: 2014-06-14 03:08
Location: India
Contact:

Re: Prevent Insert with custom message?

Post by udayvatturi » 2015-06-29 05:24

Hi,
May be you can try this

Code: Select all


$id=sqlValue("select id from <table name > where show='{$data['show']}'' and dog='{$data['dog']}'' and class='{$data['class']}''");
if($id==null)
return true;
else
return false;

Querying the database table for the same values given, if id is returned , means there is a record with same values if null is returned ,means there is no record with the given values.

User avatar
gregsintx
Posts: 15
Joined: 2015-05-09 06:25

Re: Prevent Insert with custom message?

Post by gregsintx » 2015-06-29 05:39

Hi Uday thanks for the reply.
I had tried something like that though what I'd like to be able to do is present a message to the user like "You have already added an entry for this dog" versus just not accepting the input. I cant find where I would be able to put some message to the user that would be helpful for them to know why their entry isnt accepted.

udayvatturi
AppGini Super Hero
AppGini Super Hero
Posts: 85
Joined: 2014-06-14 03:08
Location: India
Contact:

Re: Prevent Insert with custom message?

Post by udayvatturi » 2015-06-29 10:08

This will work.

Code: Select all


// hooks code 
$id=sqlValue("select id from <table name > where show='{$data['show']}'' and dog='{$data['dog']}'' and class='{$data['class']}''");
if($id==null)
{
	return true;
}
else
{
	 $_SESSION['custom_err_msg']= "<Message You want to display>";
	return false;
}




// find the following code  in incCommon.php
elseif($_REQUEST['record-added-error'] != ''){
				$msg = $Translation['Couldn\'t save the new record'];
				$class = 'alert-danger';
				$fadeout = false;
			}
			
// replace with
elseif($_REQUEST['record-added-error'] != ''){
				$msg = $_SESSION['custom_err_msg'];
				unset($_SESSION['custom_err_msg']);
				$class = 'alert-danger';
				$fadeout = false;
			}


User avatar
gregsintx
Posts: 15
Joined: 2015-05-09 06:25

Re: Prevent Insert with custom message?

Post by gregsintx » 2015-06-29 15:10

Thanks Uday - using a Session variable is a great idea! I am going to give this a shot.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Prevent Insert with custom message?

Post by onoehring » 2019-08-22 10:51

Hi,

I am trying to set up custom error messages, using the method suggested here with session variables.
But I have a problem, so I am turning to the community.

In a table hook _before_update I check for certain things. If the check fails, I return FALSE (cancel the update) and write a session variable:

Code: Select all

$_SESSION['custom_err_msg']= $Translation['Couldn\'t save changes to the record'] . " here my own error message.";
In the hooks/header-extras.php I try to use this code to check for the variable, if it exists, output the message (nicely formated).

Unfortunately this fails.
I have been using some log file and it seems the header-extras is used twice for rendering the page. It seems further, that session variable is found in the first run, then unset, and only the second run of header-extras.php is actually used to show output on the page. This results in no message on screen.

my code in hookd/header-extras.php

Code: Select all

<?php		
	if (isset($_SESSION['custom_err_msg'])) {
		$customError ='<div class="row"><div id="customErrorMessage" class="alert alert-dismissable alert-danger" style="padding-top: 6px; padding-bottom: 6px;">'.$_SESSION['custom_err_msg'].'</div></div>';

		echo $customError;				
		unset($_SESSION['custom_err_msg']);
	}
?>
Has anyone accomplished custom error messages using this (or some other) method?
What am I doing wrong?

Hint: When I do NOT use the unset command, the error is shown - but only my own error message, not the language string. (It makes no difference if I remove the translation string.)
Olaf

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Custom error message with $SESSION does not work

Post by onoehring » 2019-08-27 11:08

Hi,

I decided to push my request by rephrasing the subject.
Does anyone have a suggestion how to get it (see my question above) to work?

Olaf

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Prevent Insert with custom message?

Post by pbottcher » 2019-08-28 13:20

Hi Olaf,

nice one :-).

The issue is, that the header-extras.php gets called twice and hence you delete your Sessionvariable after the first time the header-extras is called. So when it is called the second time, the Session variable does not exist any more and your if statement fails.
you may try:

Code: Select all

	if (isset($_SESSION['custom_err_msg'])) {
		$customError ='<div class="row"><div id="customErrorMessage" class="alert alert-dismissable alert-danger" style="padding-top: 6px; padding-bottom: 6px;">'.$_SESSION['custom_err_msg'].'</div></div>';
		if (isset($_SESSION['clear_message'])) {
			unset($_SESSION['custom_err_msg']);
			unset($_SESSION['clear_message']);
		}
		$_SESSION['clear_message']=1;
	}
But you could als try to overwrite the message that is already generated by something like

Code: Select all

	if (isset($_SESSION['custom_err_msg'])) {
		echo "<script>\$j(function() { \$j('[id^=notification]').text('".$_SESSION['custom_err_msg']."');});</script>";
		if (isset($_SESSION['clear_message'])) {
			unset($_SESSION['custom_err_msg']);
			unset($_SESSION['clear_message']);
		}
		$_SESSION['clear_message']=1;
	}
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Prevent Insert with custom message?

Post by onoehring » 2019-08-28 15:37

Hi pbötcher,

thank you for your answer. I will check this and report back.

Olaf

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Prevent Insert with custom message?

Post by onoehring » 2019-08-28 16:56

Hi pbötcher,

for now, neither of your suggestions works (PS: your 1st suggestion was missing an echo $customError , but this is not the problem).
As the session variable is set in a function I am thinking of using session_start() in that function, and again in the header-extras.php - just in case, the session has not been resumed at this time (even though, I am pretty sure I had checked that before.

Found this " force your session_starts everywhere and anywhere you invoke the $_SESSION superglobal." here http://www.codingforums.com/php/207845- ... ction.html maybe it helps to really start the session in every extra file.

If anyone has some other suggestions, they are very welcome.

Have to go. I will test my assumptions tomorrow or the day after and report back.

Olaf

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Prevent Insert with custom message?

Post by pbottcher » 2019-08-28 19:14

Hi,

what did you get? It worked (with the echo of course) in my environment.
Can you post the code you have.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Prevent Insert with custom message?

Post by onoehring » 2019-08-29 13:46

Hi pbötcher,

first - I did ass session_start to the header-extra and my hooks/tablename.php file as the very first line.

I am using this code in my header-extras now:

Code: Select all

	echo "<!-- before test -->";
	echo "A s[scounter]: ".$_SESSION['sess_counter'];
	$_SESSION['sess_counter'] = $_SESSION['sess_counter'] + 2;
	if ($_SESSION['sess_counter'] >= 2){
	if (isset($_SESSION['custom_err_msg'])) {
		$customError ='<div class="row"><div id="customErrorMessage" class="alert alert-dismissable alert-danger" style="padding-top: 6px; padding-bottom: 6px;">'.$_SESSION['custom_err_msg'].'</div></div>';
		echo $customError;	
		
			unset($_SESSION['custom_err_msg']);
			unset($_SESSION['sess_counter']);
		}
	}
	echo "B s[scounter]: ".$_SESSION['sess_counter'];
	echo "<!-- after test -->";
and this code is in the TABLENAME_before_update function ( $myReturnValue is set to either TRUE or false depending on the checks I am doing):

Code: Select all

	if ($myReturnValue === TRUE) { 
	$_SESSION['sess_counter'] = 0; }
	else {
		$_SESSION['sess_counter'] = $_SESSION['sess_counter'] + 2;
	}
	return $myReturnValue;
} //closes the TABLENAME_before_update  function
and this results when I reproduce an error on purpose:
ec93.png
ec93.png (9.37 KiB) Viewed 11168 times
Source of this:

Code: Select all

<!-- before test -->A s[scounter]: 2B s[scounter]: 4<!-- after test --><!-- END Header Extras  -->				<!-- Add header template below here .. -->


Olaf

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Prevent Insert with custom message?

Post by pbottcher » 2019-08-29 14:46

Hi Olaf,

as I sayed, the header-extras.php is called twice. So according to your logic, you execute the
if (isset($_SESSION['custom_err_msg'])) {
the first time the header-extras is called and then you unset your unset($_SESSION['custom_err_msg']);
As the output is not directly displayed, you will not see anything from the first passing.
The second time you pass into the header-extras.phh and the $_SESSION['custom_err_msg'] is not existing as you unset it already.
Hence the output you see is correct.
You need to make sure that your $_SESSION['custom_err_msg'] exists, when you execute the header-extras.php the second time.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Prevent Insert with custom message?

Post by onoehring » 2019-08-30 08:12

Hi pbötcher,

I was able to create a working solution. Thank you for your help.

This is my solution to show a custom error message on the page if the record could not be saved.
The solution here applies to updating a record, but can easily be changed to inserting a new one or deleting.

In the hooks/TABLENAME -> _before_update function, I am setting the variable $myReturnValue to either TRUE (if the record can be saved) or FALSE (if the record must not be saved).
When setting that variable, I also set a session variable custom_err_msg to the message, I want to show on the resulting page (in my case, top of the TABLENAME_view). The code looks like this

Code: Select all

function TABLENAME_before_update(&$data, $memberInfo, &$args){
	// first initialize the variable to some default:
	$myReturnValue = TRUE;	

	//... more code in the function ...

	if ($OnTopCounter <> 0){			//here I do my checking if the record can be saved (=updated) or not					
		$_SESSION['custom_err_msg'] = 'You can not do this because ........';
		$myReturnValue = FALSE;
	}

	//Prepare user defined error. Shown in header-extras
	if ($myReturnValue === TRUE) { 
		unset($_SESSION['custom_err_msg']);				//no custom error, when record can be saved
	}

	return $myReturnValue;      	// returns from the _before_update function. If TRUE, record will be saved, if false AG will show error AND my error
}
The code above prepares for the /hooks/header-extras.php code which checks for the variable, if needed, outputs the error and resets the values. The trick (at least for me) is: header-extras seems to be run more than once (why?). The session variable custom_err_msg must be reset only after we are sure, the error has been shown, thus I needed to fiddle around with the session variable custom_err_shown:

Code: Select all

if (isset($_SESSION['custom_err_msg'])) {
	$customError ='<div id="customErrorMessage" class="custmErrMsg alert alert-dismissable alert-danger">'.$_SESSION['custom_err_msg'].'</div>';
	echo $customError;			
	if ($_SESSION['custom_err_shown'] == 1){
		unset($_SESSION['custom_err_msg']);
		$_SESSION['custom_err_shown'] = 0;
	} 
	else {
		$_SESSION['custom_err_shown'] = 1;
	}				
}
Almost forgot: My css for the IDs and classes I use for the custom error message:

Code: Select all

#customErrorMessage {
	margin-top: 2em !important;
}	
.custmErrMsg{}
Thank you for your help in solving this problem. Is this worth to be posted n the tips& tricks section as well?
Olaf

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Prevent Insert with custom message?

Post by pbottcher » 2019-08-30 08:48

Hi Olaf,

glad to see it works, also I do not really see much of a difference between your solution and the solution already mentioned in
viewtopic.php?f=7&t=1740#p10877
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

Post Reply