Valery Posted October 2, 2013 Share Posted October 2, 2013 Hello everybody, I am playing with the InputfieldSelectMultiple module, and I was wondering how to set the "selected" attribute for an Option? I have the following code: $field = $modules->get("InputfieldSelectMultiple"); $field->label = "Multiple select"; $field->attr('id+name', 'options'); $field->setAttribute('multiple', 'multiple'); $field->setAttribute('size', '8'); $field->addOption('121', '121-22', array('selected' => 'selected')); Which gives me this: <option selected='selected' selected="selected" value='121'>121-22</option> While it technically makes an option selected, this also produces redundant HTML code which does not validate. So, what's the right way to use addOption() so that the output is more like <option selected value=...> ? Thanks. Link to comment Share on other sites More sharing options...
Soma Posted October 2, 2013 Share Posted October 2, 2013 Just set the value of the field like: $field->attr("value", "121-22"); or multiple I think: $field->attr("value", array("121-22")); 1 Link to comment Share on other sites More sharing options...
Valery Posted October 2, 2013 Author Share Posted October 2, 2013 Thanks, Soma. Here's what it looks like now: $field = $modules->get("InputfieldSelectMultiple"); $field->label = "Multiple select"; $field->attr('id+name', 'options'); $field->setAttribute('multiple', 'multiple'); $field->setAttribute('size', '8'); $field->addOption('121', '121-22'); $field->attr("value", "121-22"); $field->addOption('122', '122-23'); $form->append($field); And here's what it does: <select id="options" name="options[]" multiple="multiple" size="8"> <option selected='selected' value='121'>121-22</option> <option value='122'>122-23</option> </select> Link to comment Share on other sites More sharing options...
marcus Posted October 6, 2014 Share Posted October 6, 2014 For the ProcessWire Recipe contribution form I want to offer a possibility to reference existing tags. The following code doesn't work and I'm not sure why, since $pages->find("template=tag, include=all") should return an array. $tagSelectField = $modules->get("InputfieldSelectMultiple"); $tagSelectField->label = _("Tags:"); $tagSelectField->attr("value", $pages->find("template=tag, include=all")); $tagSelectField->attr('id+name', 'recipeTags'); $form->append($tagSelectField); The multiselect renders empty (and yes, there are non-hidden, published pages with "tag" template). Can somebody give me a pointer? Link to comment Share on other sites More sharing options...
kongondo Posted October 6, 2014 Share Posted October 6, 2014 @Marcus, Answered here: https://processwire.com/talk/topic/4646-inputfieldselectmultiple-how-to-set-selected-for-option/ Link to comment Share on other sites More sharing options...
marcus Posted October 6, 2014 Share Posted October 6, 2014 @Marcus, Answered here: https://processwire.com/talk/topic/4646-inputfieldselectmultiple-how-to-set-selected-for-option/ That's what I did, setting the PageArray as value, $tagSelectField->attr("value", $pages->find("template=tag, include=all")) - still it doesn't work. Link to comment Share on other sites More sharing options...
kongondo Posted October 6, 2014 Share Posted October 6, 2014 (edited) $tagSelectField = $modules->get("InputfieldSelectMultiple"); $tagSelectField->label = __("Tags:");//note the translation string change from your original code $opts = $pages->find('template=tag, include=all'); foreach($opts as $opt) { $tagSelectField->addOption($opt->id, $opt->title); } $tagSelectField->attr('id+name', 'recipeTags'); $form->apend($tagSelectField); Edit. There might be lots of tags though...so you might want to use a different inputfield, maybe autocomplete...But don't know if autocomplete will work in the frontend! Edited October 6, 2014 by kongondo 3 Link to comment Share on other sites More sharing options...
marcus Posted October 6, 2014 Share Posted October 6, 2014 Ah, I see - thanks! Yes, InputfieldSelectMultiple was just a starting point in order to get a grip on programmatically creating fields with multiple values supplied - eventually going for a more usable solution. Thank you as well for pointing me towards the "double underscore translation" Link to comment Share on other sites More sharing options...
kongondo Posted October 6, 2014 Share Posted October 6, 2014 ...Thank you as well for pointing me towards the "double underscore translation" Read more here http://processwire.com/api/multi-language-support/code-i18n/ There are differences when working in a class versus in a template file... Link to comment Share on other sites More sharing options...
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