#1 January 9, 2015 12:59pm

jmason03
Member
Registered: January 9, 2014
Posts: 123

Saving a 0 using a Module Form

I have an image slider that I'm building as a module. One of the fields that I added was a page ID, which would be used to identify the page you want the slider to appear on. One of the pages I'll be using it on is our homepage, but when I tried to save 0 in that field it would always get stripped out and saved as NULL. I think the problem is in the auto-modules class, specifically the sanitizeData function. In the part where integers are sanitized there is an that reads

if ($val !== 0 && !$val && $allow_null == "YES")

Since my 0 is considered a string (coming from the POST through a text input), it passes test one due to the type strictness, it passes test two because the 0 is loosely considered to be false, and then my $allow_null is set to yes so the third passes and my value is set to NULL.

I would imagine the way to fix this is either remove the second = off the first test so it'll accept strings/ints or change the second to check type as well with $val !== false.

Offline

#2 January 9, 2015 1:58pm

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Saving a 0 using a Module Form

I'll need to think on this one a bit, but I'll add it to the bug fix list! PHP's loose conversions are tricky. I think maybe this condition would be best:

if ($allow_null == "YES" && ($val === null || $val === false || $val === "")) {

That way we check for explicitly "empty" rather than falsey values. I guess there could be a case where someone really wants to store an empty string rather than NULL... but those edge cases might just have to be left to custom code. Thoughts?

Offline

#3 January 9, 2015 6:28pm

jmason03
Member
Registered: January 9, 2014
Posts: 123

Re: Saving a 0 using a Module Form

I think you're right. The way you structured yours is better, be as specific as possible when checking for empty values so that everything else that should be valid can go through.

Offline

#4 January 12, 2015 2:22pm

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 974

Re: Saving a 0 using a Module Form

Cool, should be fixed in this commit:
https://github.com/bigtreecms/BigTree-C … 4aa6f0c0db

I gave it a test and it seems to work properly now but let me know if you still run into any related issues smile

Offline

Board footer

Powered by FluxBB

The Discussion Forum is not available on displays of this size.