Validate field before insert or after insert using hook
Validate field before insert or after insert using hook
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)
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
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;
AppGini plugins to add more power to your apps:
- DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
- Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
- Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.
Re: Validate field before insert or after insert using hook
Thanks for the reply. Am trying it own now. I will revert!
Re: Validate field before insert or after insert using hook
Still doesn't work.
$email_exists = mysql_query("select email from applicants_and_tenants where email='{$data['email']}'");
if($email_exists) return false;
$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
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
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;
}
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
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
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
Why are you using mysql_query() instead of sqlValue()?
AppGini plugins to add more power to your apps:
- DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
- Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
- Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.
Re: Validate field before insert or after insert using hook
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....
$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
Try
$email_exists = sqlValue("select count(1) from tablename where email='".$data['email']."'");
if($email_exists > 0) return false;
return true;
$email_exists = sqlValue("select count(1) from tablename where email='".$data['email']."'");
if($email_exists > 0) return false;
return true;
Bert
I am using Appgini 5.75
I am using Appgini 5.75
Re: Validate field before insert or after insert using hook
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
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;
}
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;
}
Bert
I am using Appgini 5.75
I am using Appgini 5.75
Re: Validate field before insert or after insert using hook
Hello,a.gneady wrote: ↑2016-02-16 22:30In 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;
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
Hi,
can you try to use varchar as datatype instead.
can you try to use varchar as datatype instead.
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.
Re: Validate field before insert or after insert using hook
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
Kind regards,
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
AppGini 24.10 Revision 1579 + all AppGini Helper tools
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
[code]...[/code]
blocks for better readabilityAppGini 24.10 Revision 1579 + all AppGini Helper tools
Re: Validate field before insert or after insert using hook
Genius. Function perfectly now. Thank you so much kind Sir.