public function __invoke(Field $field, array $options, array $htmlAttributes = []) : string { $htmlAttributes['id'] = 'input.' . $field->getKey(); if (array_key_exists('multiple', $htmlAttributes)) { $htmlAttributes['name'] = $field->getKey() . '[]'; $selectedValues = $field->getNestedValues(); } else { $htmlAttributes['name'] = $field->getKey(); $selectedValues = [$field->getValue()]; } $document = new DOMDocument('1.0', 'utf-8'); $select = $document->createElement('select'); $document->appendChild($select); $this->addAttributes($select, $htmlAttributes); $this->addOptions($document, $select, $options, $selectedValues); return $document->saveHTML($select); }
public function testGetNestedValues() { $field = new Field('foo', '', new FormErrorSequence(), Data::fromFlatArray(['foo[0]' => 'bar0', 'foo[1]' => 'bar1', 'foo[1][baz]' => 'bar2'])); $this->assertSame(['bar0', 'bar1'], $field->getNestedValues()); }