Jump to content

InputfieldSelectMultiple: How to set "selected" for <OPTION>?


Valery
 Share

Recommended Posts

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

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

  • 1 year later...

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

$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 by kongondo
  • Like 3
Link to comment
Share on other sites

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

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

×
×
  • Create New...