/** * @covers ::generateInput * @group Fieldset */ public function testGenerateSingleElement() { $type = 'submit'; $name = 'send'; $value = 'GO!'; $config = ['name' => $name, 'value' => $value, 'attributes' => ['blue' => 'green']]; $submit = $this->builder->generateInput($type, $config); $this->assertInstanceOf('Fuel\\Fieldset\\Input\\Submit', $submit); $this->assertEquals($name, $submit->getName()); $this->assertEquals(['value' => $value, 'name' => $name, 'type' => $type, 'blue' => 'green'], $submit->getAttributes()); }
/** * Generates a form structure based on the given data. * * @param string $data Class name of a ORM model * * @return InputElement[]|Fieldset|Form * * @since 2.0 */ public function generate($data) { // Get the model properties $properties = $data::properties(); $elements = []; // Loop over each one and create+add them foreach ($properties as $name => $propertyConfig) { // If type = false then do not add. $type = \Arr::get($propertyConfig, 'form.type', 'text'); if ($type === false) { continue; } // Build up a config array to pass to the parent $config = ['name' => $name, 'label' => \Arr::get($propertyConfig, 'label', $name), 'attributes' => \Arr::get($propertyConfig, 'form.attributes', [])]; $content = \Arr::get($propertyConfig, 'form.options', false); if ($content !== false) { foreach ($content as $value => $contentName) { if (is_array($contentName)) { $group = ['type' => 'optgroup', 'label' => $value]; foreach ($contentName as $optValue => $optName) { $group['content'][] = ['type' => 'option', 'value' => $optValue, 'content' => $optName]; } $config['content'][] = $group; } else { $config['content'][] = ['type' => 'option', 'value' => $value, 'content' => $contentName]; } } } $instance = $this->builder->generateInput($type, $config); $elements[$name] = $instance; } // Create the wrapper element if ($this->wrapIn === null) { return $elements; } $wrapperClass = 'Fuel\\Fieldset\\' . ucfirst($this->wrapIn); /** @var InputContainer $wrapper */ $wrapper = new $wrapperClass(); $wrapper->setContents($elements); return $wrapper; }