Page 1 of 1

Save record only if a checkbox is checked.

Posted: 2023-12-06 15:39
by A Bindi
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.

Re: Save record only if a checkbox is checked.

Posted: 2023-12-07 08:31
by SkayyHH
This works for me if I set the checkbox to required.

Re: Save record only if a checkbox is checked.

Posted: 2023-12-07 12:42
by A Bindi
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.

Re: Save record only if a checkbox is checked.

Posted: 2023-12-08 01:43
by zibrahim
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.

Re: Save record only if a checkbox is checked.

Posted: 2023-12-11 14:21
by A Bindi
Thank you a LOT zibrahim !!!

ALex.

Re: Save record only if a checkbox is checked.

Posted: 2023-12-12 00:53
by zibrahim
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.

Re: Save record only if a checkbox is checked.

Posted: 2023-12-29 20:50
by xbox2007
thanks a lot