Jump to content

set() on on inputfieldSelectMultiple doesn't remove unchecked options


thomas
 Share

Recommended Posts

Hello,

I'm trying to build a frontend form using on of my Page fields, but since I don't always want to offer every option of the page field, I tried rebuilding it. So instead of simply doing:

$form->append($edit->getInputfields()->get('myPageField'));

I used this:

$field = $modules->get('InputfieldCheckboxes');
$field->attr('name', 'options');
$field->attr('id', 'Inputfield_options');
$field->set('optionColumns',4);
foreach($options as $ci) {
	$checked = array();
	if($edit->options->has($ci)) $checked['checked']='checked';
	$field->addOption($ci->id,$ci->title,$checked);
}
$form->append($field);

(Where $edit is the page that's being edited, 'options' is an actual field in $edit's template and $options is a page array with some or all of the options in 'options')

Now this works fine, but when I save the form

foreach($edit->fields as $f) {
           $edit->set($f->name, $form->get($f->name)->value);
}

'options' are correctly added when checked, but never removed when unchecked. I compared the form and the post data to when I simply use the 'options' field and I can't find any differences. I can loop over the options field and remove them manually but I just don't understand why it works with the stock field but doesn't when I rebuild it.

Can anyone shed some light?

Thanks,

thomas

Link to comment
Share on other sites

A better approach is to add all your options in your foreach with $field->addOption($key, $value). But don't set any "checked" attributes. After that foreach, set the 'value' attribute: $field->attr('value', array or PageArray). 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...