<![CDATA[ Default-checking dynamic radios/checkboxes inserted via onBeforeFormParse - My Forums]]> https://forums.modx.com/thread/?thread=97098 <![CDATA[Default-checking dynamic radios/checkboxes inserted via onBeforeFormParse]]> https://forums.modx.com/thread/97098/default-checking-dynamic-radios-checkboxes-inserted-via-onbefroreformparse#dis-post-525238 while using eForm 1.4.6 in MODX 1.0.15 I encountered the following problem.
There is a placeholder in my eForm template which is replaced by a bunch of radio buttons using the eformOnBeforeFormParse event. The problem is, that whenever a validation error occurs, all checked radio buttons weren't checked anymore.
The template looks like this:
<form id="test">
  <input type="text" name="fullname" eform="Name::1">
  [+placeholder+]
</form>

The function that is called by eformOnBeforeFormParse looks like this:
function makeRadios(&$fields,&$templates) {
  for($i=1;$i<=31;$i++) {
    $ph .= '<input type="radio" name="day['.$i.']" id="day_'.$i.'_A" value="A"><label for="day_'.$i.'_A">A</label>';
    $ph .= '<input type="radio" name="day['.$i.']" id="day_'.$i.'_B" value="B"><label for="day_'.$i.'_B">B</label>';
  }
  $templates['tpl'] = str_replace('[+placeholder+]',$ph,$templates['tpl']);
  return true;
}

which renders the radios correctly as
<input type="radio" name="day[1]" id="day_1_A" value="A"><label for="day_1_A">A</label>
<input type="radio" name="day[1]" id="day_1_B" value="B"><label for="day_1_B">B</label>
<input type="radio" name="day[2]" id="day_2_A" value="A"><label for="day_2_A">A</label>
<input type="radio" name="day[2]" id="day_2_B" value="B"><label for="day_2_B">B</label>
...

I tried to set the checked-attribute inside the makeRadios-function, but eForm removes all checked-attributes while parsing. Than I tried to set the value of $fields['day'][1] manually via an eformOnBeforeFormParse-function, but again no radios were checked.

Now the trick. In a function called by eformOnBeforeFormParse I have to set a value to $fields['day1'] instead of $fields['day'][1].
function setValues(&$fields) {
  if(is_array($fields['day'])) {
    foreach($fields['day'] as $i => $v) {
      $fields['day'.$i] = $v;	
    }
  }
  return true;
}

So it was a problem with having square brackets in the name-attribute of my radio buttons. Obviously eForm changes "day[1]" to "day1".]]>
kulmjens May 06, 2015, 12:18 PM https://forums.modx.com/thread/97098/default-checking-dynamic-radios-checkboxes-inserted-via-onbefroreformparse#dis-post-525238