After upgrading to PHP 7.2, we noticed, with all versions of webform, ending with the latest dev version, that we now receive a PHP warning for every field value of a submitted webform.
The exact error is (repeated for every field):
Warning: count(): Parameter must be an array or an object that implements Countable in _webform_client_form_validate() (line 3105 of /oursitepath/sites/all/modules/contrib/webform/webform.module).
When reviewing http://php.net/manual/en/migration72.incompatible.php it is seen that you can no longer use count() in the scenario it's being used on line 3105.
The notes in that section state:
// Make sure a value is passed when the field is required.
// A simple call to empty() will not cut it here as some fields, like
// checkboxes, can return a valid value of '0'. Instead, check the
// length if it's a string, and the item count if it's an array. For
// radios, FALSE means that no value was submitted, so check that too.
So, it seems that this count() was being used because nothing else would work. What should we do here instead?
Thanks!
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | webform-php72_count-2953662-18.patch | 1.07 KB | liam morland |
| #15 | webform-php72-count-2953662-15.patch | 986 bytes | g089h515r806 |
| #12 | webform-php72-count-2953662-12.patch | 955 bytes | g089h515r806 |
| #6 | webform-php72-count-2953662.patch | 1.02 KB | crashtest_ |
| #2 | php72-count-2953662.patch | 1.04 KB | crashtest_ |
Comments
Comment #2
crashtest_ commentedWell, my idea for this one is to add an is_array() in the if statement before the count().
Patch provided.
Comment #4
liam morlandThanks for the patch. Please re-roll in a webform-only repository.
If would be helpful to check for other examples of this problem in webform.
Comment #5
liam morlandComment #6
crashtest_ commentedHere is an updated patch against the git repo on branch 7.x-4.x.
Comment #7
liam morlandComment #9
liam morlandWhen the variable is not countable, count() returns 0 if it is NULL and 1 otherwise. Adding is_array() does not cause this code to do the same thing.
The patch also adds an extra empty line which it should not.
Comment #10
crashtest_ commentedHrm, well, could you suggest a method that would work? The method I proceeded with removes the warnings, but I am sure may not work as you expect. What method do you think might prevent this warning?
Comment #11
liam morland$elements['#value']is sometimes an array and sometimes not. That conditional just needs re-think so that it does the same thing as it does now, but without ever calling count() when it's not an array.Comment #12
g089h515r806 commentedhere is a new patch.
Comment #13
g089h515r806 commentedComment #15
g089h515r806 commentedadd case '0'
Comment #16
ac#15 works on 7.x-3.x (manually patched) and 7.x-4.x.
Comment #17
acComment #18
liam morlandHere is another way to do this which I think is more clear. Please check that the code does the same thing as before.
Comment #19
sgourebi commentedPatch webform-php72_count-2953662-18.patch work for me on D7.59 and php 7.2
Comment #20
pipicom commentedAlso for me the patch webform-php72_count-2953662-18.patch works successfully on D7.59 and php 7.2.
Comment #22
liam morlandThanks everyone!