#1 December 12, 2014 12:14am

jmason03
Member
Registered: January 9, 2014
Posts: 123

Form Builder - Checkboxes with Identical Values

I think I found a bug in the Form Builder module when a list of checkboxes is created with the same input name and value. In the process-types/checkbox.php file you use the in_array function on line 17 to search the master list of all checkboxes, for that particular input, and if the value in the POST array matches one of those you assume it is checked. The problem is that if two or more checkboxes share the same value but only one of them is checked, the search can't distinguish the difference and returns true on all of them.

I think I worked out a solution for it. If you change line 17 to if(array_key_exists($key, $value) && $value[$key] == $v) and the foreach on line 14 to include the key, $key => $item. Then in the draw-types/checkbox.php you implement a counter starting at 0 with the value being placed inside the [] in the input name this will create an array structure in the POST that matches the structure of the master list. Then, you can compare each value using the same key, that way regardless of the value you are always looking at the right field.

Hope this makes sense, and would love to know if you think this is a viable solution or if you see any issues/alternatives that would work better.

Thanks!

Offline

#2 December 12, 2014 11:25am

jmason03
Member
Registered: January 9, 2014
Posts: 123

Re: Form Builder - Checkboxes with Identical Values

I just realized this problem also affects when checkboxes try to repopulate with POST data when another field has an error. If you change /draw-types/checkbox.php on line 27, the if statement for adding checked="checked". If that is changed to if ((is_array($default) && array_key_exists($key, $default) && $default[$key] == $item["value"]) || ($default === false && $item["selected"])) then it uses the same array structure as $item to match the values.

Offline

#3 December 12, 2014 11:35pm

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

Re: Form Builder - Checkboxes with Identical Values

It's been a long time since I looked into all the drawing and processing code for Form Builder, but I've been rewriting the admin code for 4.2 so I'll do another pass on this as well!

What is the use case for having two checkboxes with the same value? Looking at the code right now it looks like theoretically you could get somewhat useful data in the email, but the stored array data would have ambiguous information unless it was compared against the labels. I'm happy to fix this in the 4.2 release, just want to know how it's being used by others so I can improve it even more if I'm missing out on an approach I don't understand smile

Thanks!

Offline

#4 December 16, 2014 11:34am

jmason03
Member
Registered: January 9, 2014
Posts: 123

Re: Form Builder - Checkboxes with Identical Values

Actually I have been thinking about it and with the way you have it setup I can't think of a use case where you would need multiple checkboxes with the same value. I think I just got a little over zealous and figured I'd try and fix it before thinking to see if having it setup like you do would actually be an issue. Oh well!! Thanks for looking at it!

Offline

Board footer

Powered by FluxBB

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