Example #1
0
 /**
  * Display form field. Allowed type: text, password, textarea, checkbox, select, checkboxes, file, captcha, email, hidden
  * @param $object
  * @param $type
  * @param null|array $property
  * @param null|string $helper
  * @param null|string $layerFile
  * @return null|string
  * @throws NativeException
  * @throws SyntaxException
  */
 public function field($object, $type, $property = null, $helper = null, $layerFile = null)
 {
     if ($this->model === null) {
         if (App::$Debug !== null) {
             App::$Debug->addMessage('Form model is not defined for field name: [' . strip_tags($object) . ']');
         }
         return null;
     }
     // can be dots separated object
     $propertyName = $object;
     if (Str::contains('.', $propertyName)) {
         $propertyName = strstr($propertyName, '.', true);
     }
     // check if model contains current tag name as property
     if (!property_exists($this->model, $propertyName)) {
         if (App::$Debug !== null) {
             App::$Debug->addMessage('Form field ["' . $object . '"] is not defined in model: [' . get_class($this->model) . ']', 'error');
         }
         return null;
     }
     // prepare layer template file path
     if ($layerFile === null) {
         switch ($type) {
             case 'checkbox':
                 $layerFile = static::$structLayer['checkbox'];
                 break;
             case 'radio':
                 $layerFile = static::$structLayer['radio'];
                 break;
             default:
                 $layerFile = static::$structLayer['base'];
                 break;
         }
     }
     // prepare labels text and label "for" attr
     $labelFor = $this->name . '-' . $propertyName;
     $labelText = $this->model->getLabel($object);
     $itemValue = $this->model->{$propertyName};
     // sounds like a dot-separated $object
     if ($propertyName !== $object) {
         $nesting = trim(strstr($object, '.'), '.');
         $labelFor .= '-' . Str::replace('.', '-', $nesting);
         $itemValue = Arr::getByPath($nesting, $itemValue);
     }
     // initialize form fields constructor and build output dom html value
     $constructor = new Constructor($this->model, $this->name, $type);
     $elementDOM = $constructor->makeTag($object, $itemValue, $property);
     // if item is hidden - return tag without assign of global template
     if ($type === 'hidden') {
         return $elementDOM;
     }
     // render output viewer
     return App::$View->render($layerFile, ['name' => $labelFor, 'label' => $labelText, 'item' => $elementDOM, 'help' => self::nohtml($helper)]);
 }