Load options from current selected resource(s).

@param string $model
@param string $idField
@param string $textField

@return $this
487347
public function model($model, $idField = 'id', $textField = 'name')
    {
        if (
            !class_exists($model)
            || !in_array(Model::class, class_parents($model))
        ) {
            throw new \InvalidArgumentException("[$model] must be a valid model class");
        }

        $this->options = function ($value) use ($model, $idField, $textField) {
            if (empty($value)) {
                return [];
            }

            $resources = [];

            if (is_array($value)) {
                if (Arr::isAssoc($value)) {
                    $resources[] = Arr::get($value, $idField);
                } else {
                    $resources = array_column($value, $idField);
                }
            } else {
                $resources[] = $value;
            }

            return $model::find($resources)->pluck($textField, $idField)->toArray();
        };

        return $this;
    }
78357
public function selected($field, $value)
    {
        $options = $this->resolver->resolveSelectOptions($field, (array) $value);

        return collect($options)->contains(function (RemoteWebElement $option) {
            return $option->isSelected();
        });
    }
548526
public function loadBy($field, $value)
    {
        // store
        $field = $this->getField($field);
        $system = $field->system;
        $default = $field->default;

        // add condition and load record
        $this->addCondition($field, $value);

        try {
            $this->loadAny();
        } catch (\Exception $e) {
            // restore
            array_pop($this->conditions);
            $field->system = $system;
            $field->default = $default;

            throw $e;
        }

        // restore
        array_pop($this->conditions);
        $field->system = $system;
        $field->default = $default;

        return $this;
    }
253477
public function telField(IFormModel $model, $property, array $options = [])
    {
        $element = $this->getField($model, $property);
        $options['id'] = $element['id'];

        return Html::telField($element['name'], $element['value'], $options);
    }
1135154
public function dependentSelect(Request $request, ModelConfigurationInterface $model, $field, $id = null)
    {
        $form = $this->getModelLogic($model, $id);

        if ($form instanceof JsonResponse) {
            return $form;
        }

        /** @var DependentSelect $element */
        $element = $form->getElement($field);

        if (is_null($element)) {
            return new JsonResponse([
                'message' => 'Element not found',
            ], 404);
        }

        $element->setAjaxParameters(
            $request->input('depdrop_all_params', [])
        );

        $options = $element->getOptions();

        if ($element->isNullable()) {
            $options = [null => trans('sleeping_owl::lang.select.nothing')] + $options;
        }

        return new JsonResponse([
            'output'   => collect($options)->map(function ($value, $key) {
                return ['id' => $key, 'name' => $value];
            }),
            'selected' => $element->getValueFromModel(),
        ]);
    }
1272533
public function importFields($model, $fields = null)
    {
        $this->model = $model;
        $this->form = $this->owner;

        if ($fields === false) {
            return;
        }

        if (!$fields) {
            if ($model->only_fields) {
                $fields = $model->only_fields;
            } else {
                $fields = [];
                // get all field-elements
                foreach ($model->elements as $field => $f_object) {
                    if ($f_object instanceof \atk4\data\Field
                        && $f_object->isEditable()
                        && !$f_object->isHidden()
                    ) {
                        $fields[] = $field;
                    }
                }
            }
        }

        if (!is_array($fields)) {
            $fields = [$fields];
        }

        // import fields one by one
        foreach ($fields as $field) {
            $this->importField($field);
        }

        // set update hook
        if (!$this->_hook_set) {
            $this->owner->addHook('update', array($this, 'update'));
            $model->addHook('afterLoad', array($this, 'setFields'));
            $this->_hook_set = true;
        }

        return $this;
    }
396996
public function serializeLoadField(Field $f, $value)
    {
        try {
            // use $f->serialize = [encode_callback, decode_callback]
            if (is_array($f->serialize) && isset($f->serialize[1]) && is_callable($t = $f->serialize[1])) {
                return $t($f, $value, $this);
            }

            // run persistence-specific un-serialization of field value
            return $this->_serializeLoadField($f, $value);
        } catch (\Exception $e) {
            throw new Exception(['Unable to serialize field value on load', 'field' => $f->name], null, $e);
        }
    }
901866
public function filterFields(FieldBag $fields)
    {
        $allFields = $this->getModule()->getOption('model_fields');

        $filteredFields = new FieldBag();
        foreach ($fields as $name => $field) {
            if (!$allFields->has($name)) {
                throw new \RuntimeException(sprintf('The field "%s" does not exist.', $name));
            }

            $filteredField = clone $field;
            foreach ($allFields->get($name)->getOptions() as $optionName => $optionValue) {
                if (!$filteredField->hasOption($optionName)) {
                    $filteredField->setOption($optionName, $optionValue);
                }
            }

            $filteredFields->set($name, $filteredField);
        }

        return $filteredFields;
    }
1164707
public function textField($model, $attribute, $htmlOptions = array())
    {
        return $this->createInput(TbHtml::INPUT_TYPE_TEXT, $model, $attribute, $htmlOptions);
    }
953205
public function setField(AbstractFormField $field)
    {
        if (!($field instanceof SelectField)) {
            throw new \Exception('The validator "' . get_class($this) . '" only works on select fields!');
        }

        $this->field = $field;
    }
216228
public function batch($model = null)
	{
		JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		// Set the model
		/** @var CategoriesModelCategory $model */
		$model = $this->getModel('Category');

		// Preset the redirect
		$this->setRedirect('index.php?option=com_categories&view=categories&extension=' . $this->extension);

		return parent::batch($model);
	}
405409
public function iFillInSelect2InputWith($field, $value)
    {
        $page = $this->getSession()->getPage();

        $this->openField($page, $field);
        $this->fillSearchField($page, $field, $value);
    }
716552
public function fillModel(&$model, \Illuminate\Http\Request $input, array $fields)
    {
        //run through the edit fields to see if we need to unset relationships or uneditable fields
        foreach ($fields as $name => $field) {
            if (!$field->getOption('external') && $field->getOption('editable')) {
                $field->fillModel($model, $input->get($name, null));
            }
            //if this is an "external" field (i.e. it's not a column on this model's table) or uneditable, unset it
            elseif ($name !== $model->getKeyName()) {
                $model->__unset($name);
            }
        }

        //loop through the fields again to unset any setter fields
        foreach ($fields as $name => $field) {
            $type = $field->getOption('type');

            if (($field->getOption('setter') && $type !== 'password') || ($type === 'password' && empty($model->{$name}))) {
                $model->__unset($name);
            }
        }
    }
1329501
public function create($model = null, $options = [])
    {
        $defaults = [
            'inputDefaults' => [
                'div' => [
                    'class' => 'form-group',
                ],
                'label' => [

                    'class' => 'control-label',
                ],
                'class' => 'form-control',
                'error' => [
                    'attributes' => [
                        'wrap' => 'p',
                        'class' => 'text-danger',
                    ],
                ],
            ],
            'class' => null,
            'role' => 'form',
        ];

        $options = Hash::merge($defaults, $options);
        return parent::create($model, $options);
    }
239562
public function importFields($model, $fields = null)
    {
        $this->model = $model;
        $this->grid = $this->owner;

        if ($fields === false) {
            return;
        }

        if (!$fields) {
            $fields = 'visible';
        }
        if (!is_array($fields)) {
            // note: $fields parameter only useful if model is SQL_Model
            $fields = $model->getActualFields($fields);
        }

        // import fields one by one
        foreach ($fields as $field) {
            $this->importField($field);
        }
        $model->setActualFields($fields);

        return $this;
    }
513257
public function load_optional_fields() {
        global $DB;
        foreach ($this->optional_fields as $field=>$default) {
            if (property_exists($this, $field)) {
                continue;
            }
            if (empty($this->id)) {
                $this->$field = $default;
            } else {
                $this->$field = $DB->get_field($this->table, $field, array('id', $this->id));
            }
        }
    }
238518
public function fillModel(&$model, $input)
    {
        $model->{$this->getOption('foreign_key')} = $input !== 'false' ? $input : null;

        $model->__unset($this->getOption('field_name'));
    }