Validate field before insert or after insert using hook

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
kiya2677
Posts: 9
Joined: 2016-02-15 23:29

Validate field before insert or after insert using hook

Post by kiya2677 » 2016-02-15 23:34

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)

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Validate field before insert or after insert using hook

Post by a.gneady » 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;
:idea: 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.

kiya2677
Posts: 9
Joined: 2016-02-15 23:29

Re: Validate field before insert or after insert using hook

Post by kiya2677 » 2016-02-17 08:10

Thanks for the reply. Am trying it own now. I will revert!

kiya2677
Posts: 9
Joined: 2016-02-15 23:29

Re: Validate field before insert or after insert using hook

Post by kiya2677 » 2016-02-17 09:48

Still doesn't work.
$email_exists = mysql_query("select email from applicants_and_tenants where email='{$data['email']}'");
if($email_exists) return false;

kiya2677
Posts: 9
Joined: 2016-02-15 23:29

Re: Validate field before insert or after insert using hook

Post by kiya2677 » 2016-02-17 09:49

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

kiya2677
Posts: 9
Joined: 2016-02-15 23:29

Re: Validate field before insert or after insert using hook

Post by kiya2677 » 2016-02-17 10:27

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;
}

kiya2677
Posts: 9
Joined: 2016-02-15 23:29

Re: Validate field before insert or after insert using hook

Post by kiya2677 » 2016-02-17 10:29

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.

kiya2677
Posts: 9
Joined: 2016-02-15 23:29

Re: Validate field before insert or after insert using hook

Post by kiya2677 » 2016-02-17 21:41

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?

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Validate field before insert or after insert using hook

Post by a.gneady » 2016-02-18 22:45

Why are you using mysql_query() instead of sqlValue()?
:idea: 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.

kiya2677
Posts: 9
Joined: 2016-02-15 23:29

Re: Validate field before insert or after insert using hook

Post by kiya2677 » 2016-02-19 09:41

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....

Bertv
Veteran Member
Posts: 65
Joined: 2013-12-11 15:59

Re: Validate field before insert or after insert using hook

Post by Bertv » 2016-02-21 18:58

Try
$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

kiya2677
Posts: 9
Joined: 2016-02-15 23:29

Re: Validate field before insert or after insert using hook

Post by kiya2677 » 2016-02-21 20:34

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

Bertv
Veteran Member
Posts: 65
Joined: 2013-12-11 15:59

Re: Validate field before insert or after insert using hook

Post by Bertv » 2016-02-22 10:23

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;
}
Bert
I am using Appgini 5.75

ganderson
Posts: 2
Joined: 2019-09-01 16:09

Re: Validate field before insert or after insert using hook

Post by ganderson » 2020-07-03 19:33

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;

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

Re: Validate field before insert or after insert using hook

Post by pbottcher » 2020-07-03 19:46

Hi,

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.

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Validate field before insert or after insert using hook

Post by jsetzer » 2020-07-03 19:51

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 [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

ganderson
Posts: 2
Joined: 2019-09-01 16:09

Re: Validate field before insert or after insert using hook

Post by ganderson » 2020-07-03 21:49

Genius. Function perfectly now. Thank you so much kind Sir.

Post Reply