Page 1 of 1
Validate field before insert or after insert using hook
Posted: 2016-02-15 23:34
by kiya2677
Hello AppGini Community.
Help please i want to customize hook, to check if record exists, things like email address. this will help to reduce redundancy of data. How do i go about. I can see the example i will proceed and extend appgini the way I want if possible)
Re: Validate field before insert or after insert using hook
Posted: 2016-02-16 22:30
by a.gneady
In tablename_before_insert hook, something like this would prevent inserting a record if email address already exists:
Code: Select all
$email_exists = sqlValue("select count(1) from tablename where email='{$data['email']}'");
if($email_exists) return false;
return true;
Re: Validate field before insert or after insert using hook
Posted: 2016-02-17 08:10
by kiya2677
Thanks for the reply. Am trying it own now. I will revert!
Re: Validate field before insert or after insert using hook
Posted: 2016-02-17 09:48
by kiya2677
Still doesn't work.
$email_exists = mysql_query("select email from applicants_and_tenants where email='{$data['email']}'");
if($email_exists) return false;
Re: Validate field before insert or after insert using hook
Posted: 2016-02-17 09:49
by kiya2677
In normal way, we usually write $_POST['email'] and assign this to a certain variable. But with hook in appgin we write {$data['email']}? is it correct
Re: Validate field before insert or after insert using hook
Posted: 2016-02-17 10:27
by kiya2677
E.g
function applicants_and_tenants_before_insert(&$data, $memberInfo, &$args){
//check for duplicate email
/**$email_exists = mysql_query("select email from applicants_and_tenants where email='{$data['email']}'");
echo $email_exists;
*/
//$data['email'] = '';
if($data['email'] == '')
{
$data['email'] = '
[email protected]';//use this email if email field was blank
return true;
}
else
{
$data['email'] = '
[email protected]'; //replace the entered email with this one
}
return true;
}
Re: Validate field before insert or after insert using hook
Posted: 2016-02-17 10:29
by kiya2677
The above example can instruct the hook, to insert new email address whenever the email field left blank and also when email field filled with email value this script instruct the hook to replace the field with the hard coded email, see the //comments above.
Re: Validate field before insert or after insert using hook
Posted: 2016-02-17 21:41
by kiya2677
Any idea guys on how to validate record before inserting to a table. Help guys. I tried all I could but it never work for me. Does hook work so properly?
Re: Validate field before insert or after insert using hook
Posted: 2016-02-18 22:45
by a.gneady
Why are you using mysql_query() instead of sqlValue()?
Re: Validate field before insert or after insert using hook
Posted: 2016-02-19 09:41
by kiya2677
I also tried
$email_exists = sqlValue("select count(1) from tablename where email='{$data['email']}'");
if($email_exists) return false;
return true;
also doesn't work....
Re: Validate field before insert or after insert using hook
Posted: 2016-02-21 18:58
by Bertv
Try
$email_exists = sqlValue("select count(1) from tablename where email='".$data['email']."'");
if($email_exists > 0) return false;
return true;
Re: Validate field before insert or after insert using hook
Posted: 2016-02-21 20:34
by kiya2677
Hi Bertv, its working thanks, but is it possible to give user a custom message instead of "Couldn't save the new record" I tried to echo "Sorry, duplicate email, try different email. Thank you!"; It doesn't work, so is it possible to give user custom message using hook, especial in before insert and before update hooks
Re: Validate field before insert or after insert using hook
Posted: 2016-02-22 10:23
by Bertv
Yes, its possible to give an extra message. What I do is the following:
1) in the tablename_proces_header: change the case 'detailview'
case 'detailview':
$header='';
if ($_SESSION['errorStr']) {
$header ='<%%HEADER%%><div class="alert alert-danger" style="padding-top: 6px; padding-bottom: 6px;"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">x</button>'.$_SESSION['errorStr'].'</div>';
$_SESSION['errorStr'] = null;
}
break;
2) change your validation code:
if($email_exists > 0) {
$_SESSION['errorStr'] = "--your error message--.";
return false;
}
Re: Validate field before insert or after insert using hook
Posted: 2020-07-03 19:33
by ganderson
a.gneady wrote: ↑2016-02-16 22:30
In tablename_before_insert hook, something like this would prevent inserting a record if email address already exists:
Code: Select all
$email_exists = sqlValue("select count(1) from tablename where email='{$data['email']}'");
if($email_exists) return false;
return true;
Hello,
I borrowed and modified this funtion for my needs. Thank you.
The following code woks fine with INT values ( 1,2,3,4,99 etc...) but I have a need for CHAR values now. Why does the function not work with CHAR values (A-11,B-24)? I changed the field datatype to CHAR in the application.
$bed_n_use = sqlValue("SELECT count(1) FROM `staydata` WHERE `Bed` = {$data['Bed']} AND `Out` IS NULL ") ;
if($bed_n_use) return false;
Return True;
Re: Validate field before insert or after insert using hook
Posted: 2020-07-03 19:46
by pbottcher
Hi,
can you try to use varchar as datatype instead.
Re: Validate field before insert or after insert using hook
Posted: 2020-07-03 19:51
by jsetzer
If column
Bed is alphanumeric, you should try with single quotes around {$data["Bed"]}
Code: Select all
SELECT count(1) FROM `staydata` WHERE `Bed` = '{$data['Bed']}' AND `Out` IS NULL
Re: Validate field before insert or after insert using hook
Posted: 2020-07-03 21:49
by ganderson
Genius. Function perfectly now. Thank you so much kind Sir.