Save record only if a checkbox is checked.

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
A Bindi
Veteran Member
Posts: 51
Joined: 2018-01-04 18:45

Save record only if a checkbox is checked.

Post by A Bindi » 2023-12-06 15:39

Hello,

I have a table with a field called "approved" managed with a checkbox and I want that a record (after created or modified) can be saved only if that checkbox is checked.

Is this achievable using "before insert" / "before_update" function in the hook ?

ALex.

SkayyHH
Veteran Member
Posts: 427
Joined: 2015-04-27 21:18

Re: Save record only if a checkbox is checked.

Post by SkayyHH » 2023-12-07 08:31

This works for me if I set the checkbox to required.

A Bindi
Veteran Member
Posts: 51
Joined: 2018-01-04 18:45

Re: Save record only if a checkbox is checked.

Post by A Bindi » 2023-12-07 12:42

Ok this is a walkaround, but is there a method to check the status of a checkbox (selected/not selected) before save a record in the hook, possibily not have to use Javsacript ?

ALex.

User avatar
zibrahim
Veteran Member
Posts: 137
Joined: 2020-01-28 18:30
Location: Malaysia

Re: Save record only if a checkbox is checked.

Post by zibrahim » 2023-12-08 01:43

Hi there,
I use code like this in before_update function

Code: Select all

function TABLENAME_before_update(&$data, $memberInfo, &$args) {

	// if checkbox_field is not checked, don't allow updating it
	if ($data['checkbox_field'] <> 1) {
		$args['error_message'] = 'Your error message.';
		return false;
	};
}

return TRUE;
Have a nice day.
Zala.
Appgini 24.10.1579, MacOS 14.3.1 Windows 11 on Parallels.

A Bindi
Veteran Member
Posts: 51
Joined: 2018-01-04 18:45

Re: Save record only if a checkbox is checked.

Post by A Bindi » 2023-12-11 14:21

Thank you a LOT zibrahim !!!

ALex.

User avatar
zibrahim
Veteran Member
Posts: 137
Joined: 2020-01-28 18:30
Location: Malaysia

Re: Save record only if a checkbox is checked.

Post by zibrahim » 2023-12-12 00:53

Hi ALex,
Glad it helped. However, I just realised that I have made a mistake in the code.
the return TRUE; statement should be inside the before_insert function (curly bracket), not outside.
It should be like this.

Code: Select all

function TABLENAME_before_update(&$data, $memberInfo, &$args) {

	// if checkbox_field is not checked, don't allow updating it
	if ($data['checkbox_field'] <> 1) {
		$args['error_message'] = 'Your error message.';
		return false;
	};

	return TRUE;
}
Apologise for my mistake. Have a nice day.
Zala.
Appgini 24.10.1579, MacOS 14.3.1 Windows 11 on Parallels.

xbox2007
Veteran Member
Posts: 136
Joined: 2016-12-16 16:49

Re: Save record only if a checkbox is checked.

Post by xbox2007 » 2023-12-29 20:50

thanks a lot

Post Reply