public function testRender() { $element = new Select(); $this->assertEquals("<select>\n</select>", $element->render()); $element->setAttribute('id', 'test'); $this->assertEquals("<select id=\"test\">\n</select>", $element->render()); $element->setAttribute('class', 'red'); $this->assertEquals("<select id=\"test\" class=\"red\">\n</select>", $element->render()); $element->setOption('values', [10 => 'foo', 20 => 'bar']); $this->assertEquals("<select id=\"test\" class=\"red\">\n<option value=\"10\">foo</option>\n<option value=\"20\">bar</option>\n</select>", $element->render()); $element = new Select('category'); $element->setOption('valueColumn', 'id'); $collection = new Collection(); $el1 = new DummyModel(); $el1->id = 100; $el1->title = 'test100'; $collection->append($el1); $el2 = new DummyModel(); $el2->id = 200; $el2->title = 'test200'; $collection->append($el2); $element->setOption('values', $collection); $this->assertEquals("<select name=\"category\">\n<option value=\"100\">test100</option>\n<option value=\"200\">test200</option>\n</select>", $element->render()); $element->setAttribute('id', 'test'); $this->assertEquals("<select name=\"category\" id=\"test\">\n<option value=\"100\">test100</option>\n<option value=\"200\">test200</option>\n</select>", $element->render()); $element->setOption('null', true); $this->assertEquals("<select name=\"category\" id=\"test\">\n<option value=\"0\">----</option>\n<option value=\"100\">test100</option>\n<option value=\"200\">test200</option>\n</select>", $element->render()); $element->setSelected(200); $this->assertEquals("<select name=\"category\" id=\"test\">\n<option value=\"0\">----</option>\n<option value=\"100\">test100</option>\n<option value=\"200\" selected=\"selected\">test200</option>\n</select>", $element->render()); }
public static function blockOptionInput($name, $options, $value = null, $htmlOptions = []) { $htmlOptions['name'] = $name; switch ($options['type']) { case 'int': $input = new Int($name, $options, $htmlOptions); $input->setValue(is_null($value) ? $options['default'] : $value); return $input->render(); case 'string': $input = new Text($name, $options, $htmlOptions); $input->setValue(is_null($value) ? $options['default'] : $value); return $input->render(); case 'text': $input = new Textarea($name, $options, $htmlOptions); $input->setValue(is_null($value) ? $options['default'] : $value); return $input->render(); case 'select': $select = new Select($name, $options, $htmlOptions); $select->setOption('values', $options['values']->toArray()); $select->setSelected(is_null($value) ? $options['default'] : $value); return $select->render(); case 'select:model': $select = new Select($name, $options, $htmlOptions); $select->setOption('values', $options['model']::findAll()); $select->setSelected(is_null($value) ? $options['default'] : $value); return $select->render(); case 'select:tree': return self::selectTreeByModel($options['model'], is_null($value) ? $options['default'] : $value, $options, $htmlOptions); } }