0

I'm trying to populate a set of checkboxes based on stored form data. Unfortunately, the isSelected element only seems to return false. Here's the code:

if($field['type'] == 'checkbox'){
    $inputs = $field['inputs'];
    $count = '0';
    foreach($inputs as $input){
        if(($user_meta[$input['id']] !== '') && (isset($user_meta[$input['id']]))){
           $select = true;
        }
        else{
            $select = false;
        }
        $field['choices'][$count] = array( 
            'text' => $field['choices'][$count]['text'], 
            'value' => $field['choices'][$count]['value'] , 
            'isSelected' => $select );
        var_dump($select);
        var_dump($field['isSelected']);
        $count = $count + '1';
    }
}

The var_dump of $select correctly returns boolean true and false values where it should. But the isSelected element always returns an empty string. This is true even when I replace the $select variable with a hardcoded true value, or use 1 and 0 instead of true and false.

I'm sure I'm missing something very basic, but I've had no luck finding a solution so far. I'd be grateful for any help!

Edit

Thanks to @Barmar I have a little more information on the problem. It's failing when I try to assign a value to $field['choices'][$count]['isSelected']. The log shows "PHP Notice: Indirect modification of overloaded element of GF_Field_Checkbox has no effect". Unfortunately, I can't see why that would be a problem here, as from what I can tell that error normally comes from using __get/set methods which I'm not actually making use of in this case. I also tried solutions suggested in "Indirect modification of overloaded element of SplFixedArray has no effect" to no avail. Is there some kind of setting or configuration that might be preventing this assignment?

BTBrew
  • 11
  • 3
  • It should be `var_dump($field['choices'][$count]['isSelected'])`. – Barmar Mar 29 '17 at 22:24
  • Thanks. That does at least return a boolean value, which makes more sense to me. Still, it doesn't match the value of $select and always returns false. – BTBrew Mar 29 '17 at 22:27
  • Why are you reassigning the element with an array containing the same fields? If you just want to add `isSelected`, just do `$field['choices'][$count]['isSelected'] = $selected;` – Barmar Mar 29 '17 at 22:27
  • BTW, you should switch the order of the tests here: `if(($user_meta[$input['id']] !== '') && (isset($user_meta[$input['id']]))){` You should test if the variable isset **before** testing if its value is equal to something. – Barmar Mar 29 '17 at 22:28
  • I can't think of any reason why that would happen in the code you've posted. There must be some difference. – Barmar Mar 29 '17 at 22:30
  • Turn on `error_reporting(E_ALL)`, make sure you're accessing valid indexes. – Barmar Mar 29 '17 at 22:30
  • Hey, thanks. I still don't have a solution, but turning on error reporting revealed this error: "PHP Notice: Indirect modification of overloaded element of GF_Field_Checkbox has no effect". Some digging suggests this happens when you try to change the assignment of an element in an array you accessed via the _get method. And that is how I got this array. I'll poke around that rabbit hole and hopefully root out some gremlins there. – BTBrew Mar 30 '17 at 00:05

1 Answers1

0

I believe I spotted the error. As a note, you don't need to have the $count variable in the array creation since you are going from 0++. Also the count can be shorthanded to $count++;

It looks like this is incorrect var_dump($field['isSelected']); and you meant var_dump($field['choices'][$count]['isSelected']);

$field['choices'][] = array( 
        'text' => $field['choices'][$count]['text'], 
        'value' => $field['choices'][$count]['value'] , 
        'isSelected' => $select );
var_dump($select);
var_dump($field['choices'][$count]['isSelected']);
$count++;
Johnish
  • 81
  • 6
  • That's the same thing I said in a comment. This trivial error isn't worth writing a real answer for. – Barmar Mar 29 '17 at 22:42
  • But he also said in a comment that fixing this still doesn't work. – Barmar Mar 29 '17 at 22:42
  • `$field['choices'][]` is wrong. He's not adding to a new array, he's replacing the element in the same array. – Barmar Mar 29 '17 at 22:43
  • Good catch on $field['choices'][]; Then there's no need to reassign the values and just update isSelected directly. – Johnish Mar 31 '17 at 15:34