Hi, I'm experimenting with the latest version of FormitBuilder. Keeps getting better, thanks again for writing it Marcus!
I want to store posted form values so the form is already populated on a next visit. I use formitRetriever for this, by setting the default value in formitbuilder to [[!+fi.name]] etc. Works perfect. Except for checkbox groups, I just can't get the checked options to be checked on a next visit, while all other (text)fields are populated with the previously entered data.
First, this is how the checkboxgroup is created in formitBuilder:
$a_checkboxgroup_options =array(
array('title'=>'First','checked'=>false),
array('title'=>'Second','checked'=>false),
array('title'=>'Third','checked'=>false)
);
$o_fe_checkboxgroup = new FormItBuilder_elementCheckboxGroup('checkboxgroup','Checkboxgroup',$a_checkboxgroup_options);
I tried a lot of things in core/components/formitbuilder/model/FormElement.class.php around line 1040, working in this function:
public function outputHTML(){
$s_ret='<div class="checkboxGroupWrap">';
$i=0;
$a_uncheckedVal = $this->_uncheckedValue;
if($this->_required===true){
$a_uncheckedVal=''; // we do this because FormIt will not validate it as empty if unchecked value has a value.
}
//hidden field with same name is so we get a post value regardless of tick status, must use ID and not name
$s_ret.='<input type="hidden" name="'.htmlspecialchars($this->_id).'" value="'.htmlspecialchars($a_uncheckedVal).'" />';
foreach($this->_values as $value){
$s_ret.='<div class="checkboxWrap">';
// changed input type to checkbox
// added [] to name
$s_ret.='<div class="checkboxEl"><input type="checkbox" id="'.htmlspecialchars($this->_id.'_'.$i).'" name="'.htmlspecialchars($this->_name).'" value="'.htmlspecialchars($value['title']).'"';
$selectedStr='';
if(isset($_POST[$this->_id])===true){
//if(in_array($value['title'],$_POST[$this->_id])===true){
if(in_array($value['title'],$_POST[$this->_id])===true) {
$selectedStr=' checked="checked"';
}
}else{
if(isset($value['checked'])===true && $value['checked']===true){
$selectedStr=' checked="checked"';
}
}
$s_ret.=$selectedStr.' /></div>';
if($this->_showIndividualLabels===true){
$s_ret.='<label for="'.htmlspecialchars($this->_id.'_'.$i).'">'.htmlspecialchars($value['title']).'</label>';
}
$s_ret.='</div>'."\r\n";
$i++;
}
$s_ret.='</div>';
return $s_ret;
}
I tried some things to the 'selectedStr' variable, following the examples on RTFM and in the forums, like this:
$selectedStr = ' [[!+fi.'.htmlspecialchars($this->_id).':FormItIsChecked=`'.htmlspecialchars($value['title']).'`]]';
and this:
$selectedStr = ' [[!+fi.'.htmlspecialchars($this->_id).'=`'.htmlspecialchars($value['title']).'`:then=`checked`]]';
The first one just doesn't work (the code is correct, when I try it without the brackets I can see the correct values are printed. The second version gives me an error:
Parse error: syntax error, unexpected ')' in /xxx/public_html/core/model/modx/filters/modoutputfilter.class.php(162) : eval()'d code on line 1
Also tried some other things, like storing the checkboxgroup values in a hidden textfield by adding '[[!fi.checkboxgroup]]' as default text to the hidden field, thinking I might use that value in a comparison, but nothing seems to be stored in the hidden field.
I'm not sure the problem is with formitRetriever, formIt or formItBuilder. I do see a lot of people are struggling with storing checkbox values. I do have the snippet FormItIsChecked. I also checked the values of the checkboxgroup are posted, and they are.
Can I access the stored values from Formit directly somehow with php, so I can check against those values and add 'checked="checked"' when there is a match? Or is there a different way to do this with formitBuilder? (saw in the changelogs that FormItIsChecked was replaced with own processing, but how should I use it?).
I'm using Revo 2.2.0, Formit 2.1.2, FormitBuilder 1.3.3
Am I missing something? Thanks for any help!