Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated

Status
Not open for further replies.

beatty_t

New Member
Hey All!

I had turned on J error reporting to maximum while working on something outside of Fabrik and forgot to "dial it down" before I opened a Fabrik form. I was prompted with the following Depreciated notice regarding passing null to the trim() function in the field and jdate elements. The following addressed the errors for me and I wondered if within one of the next releases, something similar should be included? It doesn't break anything to leave as is.

Here are the full error messages:

Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /www/wwwroot/centralparking.ca/plugins/fabrik_element/jDate/jDate.php on line 1084

Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /www/wwwroot/centralparking.ca/plugins/fabrik_element/field/field.php on line 191

Within jdate.php (on line 1084), I replaced:

$value = trim($value, "'");
$value = $value == "" ? null : $value;

With:

if (!empty($value)){

$value = trim($value, "'");

} else {

$value = $value == "" ? null : $value;
}

Within field.php (on line 191), I replaced:

$bits['value'] = htmlspecialchars($value, ENT_COMPAT, 'UTF-8', false);

With:

if (!empty($value)){

$bits['value'] = htmlspecialchars($value, ENT_COMPAT, 'UTF-8', false);

}
 
I am not seeing either of these errors on my site, however, I agree these are a problem. Instead of the code you have above, can you change the first one to:
Code:
        $value = $value ?? trim($value, "'");
        $value = $value == "" ? null : $value;
and the second insert the following before the if statement:
Code:
        $value = $value ?? "";
and advise if these solve your problem. These are just cleaner ways to do what you have done.
 
I am not seeing either of these errors on my site, however, I agree these are a problem. Instead of the code you have above, can you change the first one to:
Code:
        $value = $value ?? trim($value, "'");
        $value = $value == "" ? null : $value;
and the second insert the following before the if statement:
Code:
        $value = $value ?? "";
and advise if these solve your problem. These are just cleaner ways to do what you have done.

Thanks achartier! I updated the field and jDate plugin files with your suggestions. I found that your fix for the htmlspecialchars() worked, but I got the same "Depreciated..." message again after applying your suggestion for a fix to the trim() function. Seeing that your htmlspecialchars() fix worked, I used the same thing to fix the trim() issue:
$value = $value ?? "";
$value = trim($value, "'");
$value = $value == "" ? null : $value;

I've also found a similar issue within the element plugin "internalid" (file path: root/plugins/fabrik_elements/internalid/internalid.php). Within this plugin, I get two "Depreciated..." messages.
1. "Deprecated: stripslashes(): Passing null to parameter #1 ($string) of type string is deprecated in /www/wwwroot/centralparking.ca/plugins/fabrik_element/internalid/internalid.php on line 44" -> so I added the same fix (added $value = $value ?? ""; above $value = stripslashes($value);) as I did for the trim()
2. "Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /www/wwwroot/centralparking.ca/plugins/fabrik_element/internalid/internalid.php on line 55" -> again, I added the same fix

I should likely add that my forms do allow for empty values in this use case, so if I'm editing a record, it is possible for a field to be empty/null - which is likely why things like trim(), stripslashes() and htmlspecialchars() functions would be passed an empty string for $value and trigger the messages I get when I have Error Reporting set to Maximum.

If you have any concerns with what I've done, certainly let me know.

Your help was much appreciated!

Best Regards,


Travis
 
Last edited:
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top