type = $this->getType($type); $this->name = $this->getName($name); $this->label = $label; $this->id = $id ?? $name; $this->value = $this->getValue($value, $valueKey); $this->placeholder = $this->getPlaceholder($placeholder); $this->rows = $rows; $this->remote = $remote; $this->multiple = $multiple; $this->addNew = $addNew; $this->group = $group; $this->disabled = $disabled; $this->readonly = $readonly; $this->required = $this->getRequired($required, $notRequired); $this->options = $this->getOptions($options); $this->option = $option; $this->optionKey = $optionKey; $this->optionValue = $optionValue; $this->checked = $this->getChecked($checked, $checkedKey); $this->selected = $this->getSelected($selected, $selectedKey); $this->formGroupClass = $this->getFromGroupClass($formGroupClass); $this->inputGroupClass = $this->getInputGroupClass($inputGroupClass); $this->custom_attributes = $this->getCustomAttributes(); $this->setDynamicAttributes($dynamicAttributes); $this->hideCurrency = $hideCurrency; } protected function getType($type) { if (! empty($type) && (! empty($this->type) && $type != 'text')) { return $type; } if (! empty($this->type)) { return $this->type; } } protected function getName($name) { if (! empty($name)) { return $name; } return $this->name; } protected function getValue($value, $valueKey) { if ($value != '') { return $value; } if (empty($valueKey)) { $valueKey = $this->name; } if (empty($valueKey)) { return ''; } // set model value. $model = $this->getParentData('model'); $value_keys = explode('.', $valueKey); if (count($value_keys) > 1) { $valueKey = $value_keys[0]; } if (! empty($model->{$valueKey})) { $value = $model->{$valueKey}; } if ($model instanceof Collection) { $value = $model->get($valueKey); } if (count($value_keys) > 1) { $value = $value[0]->{$value_keys[1]}; } if (empty($value) && request()->has($valueKey)) { $value = request()->get($valueKey); } return old($valueKey, $value); } protected function getPlaceholder($placeholder) { if (! empty($placeholder)) { return $placeholder; } $label = $this->label; if (! empty($this->label) && ! empty($this->label->contents)) { $label = $this->name; } if ($this->type == 'select') { return trans('general.form.select.field', ['field' => $label]); } return trans('general.form.enter', ['field' => $label]); } protected function getOptions($options) { if (! empty($options)) { if (is_array($options) && ! $this->group) { $o = collect(); foreach ($options as $key => $value) { if (is_array($value)) { $o->push((object) $value); } else { $o->push((object) [ 'id' => $key, 'name' => $value, ]); } } $options = $o; } return $options; } return []; } protected function getChecked($checked, $checkedKey) { return $this->getValue($checked, $checkedKey); } protected function getSelected($selected, $selectedKey) { return $this->getValue($selected, $selectedKey); } protected function getRequired($required, $notRequired) { if (! empty($notRequired)) { return false; } return $required; } protected function getFromGroupClass($formGroupClass) { if (! empty($formGroupClass)) { return $formGroupClass; } return $this->formGroupClass; } protected function getInputGroupClass($inputGroupClass) { if (! empty($inputGroupClass)) { return $inputGroupClass; } return $this->inputGroupClass; } protected function getCustomAttributes() { $attributes = []; if (! empty($this->required)) { $attributes['required'] = $this->required; } if (! empty($this->disabled)) { $attributes['disabled'] = $this->disabled; } if (! empty($this->readonly)) { $attributes['readonly'] = $this->readonly; } foreach ($this->custom_attributes as $key => $value) { $attributes[$key] = $value; } return $attributes; } protected function setDynamicAttributes($dynamicAttributes) { if (! empty($dynamicAttributes)) { $this->dynamicAttributes = $dynamicAttributes; } } }