thomas Posted January 17, 2013 Posted January 17, 2013 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
Soma Posted January 17, 2013 Posted January 17, 2013 You maybe have to simply do a $page->options->removeAll(), to then populate it with the checked again. 1
thomas Posted January 17, 2013 Author Posted January 17, 2013 Thanks soma, that's what I did now. I was just curious why it works for "stock" fields but not on my own ... I guess PW likes to keep some of her secrets
ryan Posted January 18, 2013 Posted January 18, 2013 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).
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now