/** * Adds an option to the select field. * * @param string $displayName The display text of the option. * @param string $value The option's value. * @param boolean $preSelected True in case, the option should be selected, false otherwise. * * @author Christian Achatz * @version * Version 0.1, 15.02.2010<br /> */ public function addOption($displayName, $value, $preSelected = false) { $option = new SelectBoxOptionTag(); $option->setContent($displayName); $option->setAttribute('value', $value); if ($preSelected == true) { $option->setAttribute('selected', 'selected'); } $this->addOptionTag($option); }
/** * Tests transformation of a complex multi-select box. */ public function testTransformation() { $select = $this->getSelectBox(); $group = new SelectBoxGroupTag(); $group->setAttribute('label', 'foo'); $option1 = new SelectBoxOptionTag(); $option1->setAttribute('value', '1'); $option1->setContent('1'); $option1->setAttribute('selected', 'selected'); $option2 = new SelectBoxOptionTag(); $option2->setAttribute('value', '2'); $option2->setContent('2'); $option3 = new SelectBoxOptionTag(); $option3->setAttribute('value', '3'); $option3->setContent('3'); $option3->setAttribute('selected', 'selected'); $option4 = new SelectBoxOptionTag(); $option4->setAttribute('value', '4'); $option4->setContent('4'); $option4->setAttribute('selected', 'selected'); $group->addOptionTag($option1); $group->addOptionTag($option2); $group->addOptionTag($option3); $select->addGroupTag($group); $select->addOptionTag($option4); $actual = $select->transform(); // general structure $this->assertContains('<select multiple="multiple" name="' . self::SELECT_BOX_NAME . '[]"', $actual); $this->assertContains('</select>', $actual); $this->assertContains('<optgroup label="foo">', $actual); $this->assertContains('</optgroup>', $actual); // selected items $this->assertContains('<option value="1" selected="selected">1</option>', $actual); $this->assertContains('<option value="2"', $actual); $this->assertContains('<option value="3" selected="selected">3</option>', $actual); $this->assertContains('<option value="4" selected="selected">4</option>', $actual); }