예제 #1
0
파일: Form.php 프로젝트: okneloper/forms
 /**
  * Universal Add function.
  * Add an element to the form. If $type is a string, it will be used as class name in one of the registered
  * namespaces (currently only Okneloper\Forms\Elements) along with other constructor arguments.
  * If an instance of Element is passed, the element is added and other arguments are ignored.
  *
  * @param string|Element $type
  * @param null $name
  * @param null $label
  * @param array $attribs
  * @return Element
  * @throws \Exception
  */
 public function add($type, $name = null, $label = null, $attribs = [])
 {
     if ($type instanceof Element) {
         $this->addElement($type);
         return $type;
     } else {
         if (!$name) {
             throw new \Exception("Name not specified");
         }
         $el = Element::factory($type, $name, $label, $attribs);
         $this->addElement($el);
         return $el;
     }
 }