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