Jump to content

Programatically setting selected=selected for Multi Select field


cosmicsafari
 Share

Recommended Posts

Hi all,

I have need to dynamically set InputFieldMultiSelect to  selected on page load based on the status of some items within a database table.

However I keep running into issues when trying to do this via InputfieldSelect::setOptionAttributes()

https://processwire.com/api/ref/inputfield-select/set-option-attributes/

Going by the above it sounds like it should be pretty straight forward, and for certain values it seems to work but not when I wanted to set it to 'selected'.

For example:

$f->setOptionAttributes(1030,['foo' => 'test']);

The above works as I would have wanted, in that it updates the option with the value 1030, to include the attribute foo="test"

But the same code above edited to the following:

$f->setOptionAttributes(1030,['selected' => 'selected']);

Doesn't seem to do anything?

I assume I'm missing something or trying to implement the 'selected' wrongly but I'm not sure how else I should approach this, any advice would be much appreciated.

 

Link to comment
Share on other sites

It's not possible to set the following attributes via setOptionAttributes() function: value, checked, selected.

https://github.com/processwire/processwire/blob/51629cdd5f381d3881133baf83e1bd2d9306f867/wire/modules/Inputfield/InputfieldSelect.module#L612

If you want to dynamically change a page field value:
 

$page->selectfield = array(1030,1031);

 

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

@kixe

On 5/22/2020 at 5:07 PM, kixe said:

It's not possible to set the following attributes via setOptionAttributes() function: value, checked, selected.

https://github.com/processwire/processwire/blob/51629cdd5f381d3881133baf83e1bd2d9306f867/wire/modules/Inputfield/InputfieldSelect.module#L612

If you want to dynamically change a page field value:
 

$page->selectfield = array(1030,1031);

 

No. That's not how it works now.

// Single select example
$page->of(false);
$page->multy_select_field = PageArray( wire('pages')->get(1111) );
$page->save();
$page->of(true);

// Multyple select example use construction $a = PageArray([ $page1, $page2, $page3 ]);
$page->of(false);
$page->multy_select_field = PageArray([ wire('pages')->get(1111), wire('pages')->get(2222) ]);
$page->save();
$page->of(true);

 

  • Like 1
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...