Pop\Form\Form::addElements PHP Method

addElements() public method

Add a form element or elements to the form object.
public addElements ( mixed $e ) : Form
$e mixed
return Form
    public function addElements($e)
    {
        if (is_array($e)) {
            $this->form->addChildren($e);
        } else {
            $this->form->addChild($e);
        }
        $children = $this->form->getChildren();
        foreach ($children as $child) {
            $attribs = $child->getAttributes();
            if ($child instanceof Element\Textarea) {
                if (isset($attribs['name'])) {
                    $this->fields[$attribs['name']] = null !== $child->getValue() ? $child->getValue() : null;
                }
            } else {
                if ($child instanceof Element\Select) {
                    if (isset($attribs['name'])) {
                        $name = strpos($attribs['name'], '[]') !== false ? substr($attribs['name'], 0, strpos($attribs['name'], '[]')) : $attribs['name'];
                        $this->fields[$name] = null !== $child->getMarked() ? $child->getMarked() : null;
                    }
                } else {
                    if ($child instanceof Element\Radio) {
                        $radioChildren = $child->getChildren();
                        if (isset($radioChildren[0])) {
                            $childAttribs = $radioChildren[0]->getAttributes();
                            if (isset($childAttribs['name'])) {
                                $this->fields[$childAttribs['name']] = null !== $child->getMarked() ? $child->getMarked() : null;
                            }
                        }
                    } else {
                        if ($child instanceof Element\Checkbox) {
                            $checkChildren = $child->getChildren();
                            if (isset($checkChildren[0])) {
                                $childAttribs = $checkChildren[0]->getAttributes();
                                if (isset($childAttribs['name'])) {
                                    $key = str_replace('[]', '', $childAttribs['name']);
                                    $this->fields[$key] = null !== $child->getMarked() ? $child->getMarked() : null;
                                }
                            }
                        } else {
                            if (isset($attribs['name'])) {
                                $this->fields[$attribs['name']] = isset($attribs['value']) ? $attribs['value'] : null;
                                if ($attribs['type'] == 'file') {
                                    $this->hasFile = true;
                                }
                            }
                        }
                    }
                }
            }
        }
        return $this;
    }

Usage Example

Example #1
0
 public function testRenderWithTemplate()
 {
     $e = new Element('text', 'username', 'Username');
     $e->setLabel('Username');
     $s = new Element('submit', 'submit', 'Submit');
     $f = new Form('/submit', 'post');
     $f->addElements(array($e, $s));
     $f->setTemplate("[{username}] [{submit}]");
     $f->username = '******';
     $form = $f->render(true);
     $this->assertContains('<form ', $form);
     $this->assertEquals('My Username', $f->username);
 }
All Usage Examples Of Pop\Form\Form::addElements