Instantiates a new FormField from our Field model or array.

@param      $field

@param null $prefix

@return mixed
@throws Exception
492104
public function field($field, $prefix = null)
    {
        if (is_array($field)) {
            $field = new Field($field);
        }

        if (! $field instanceof FieldInterface) {
            throw new Exception('Field must implement interface '.FieldInterface::class.' or be an array.');
        }

        $formField = $this->getRegistrar()->resolve($field);

        if ($prefix) {
            $formField->setPrefix($prefix);
        }

        return $formField;
    }
376780
public function getValidation($field = null)
    {

        $validation = array();
        $schema = $this->getModelSchema();

        if ($field != null)
            $schema = array($field => $schema[$field]);

        foreach ( $schema as $key => $value ) {
            if (!isset($value['validation']))
                throw new Exception('Validation missing for key: '.$field);
            $validation[$key] = $value['validation'];
        }

        if ($field != null)
            return $validation[$field];

        return $validation;
    }
967051
public function getField($field)
	{
		$result = $this->client->get("fields/{$field}");
		return $result ? new Resources\Field($this, $result) : null;
	}
540510
public static function getField($field)
    {
        return isset(self::$fields[$field]) ? self::$fields[$field] : null;
    }
1035311
public static function fromCIDRPrefix($prefixSize)
    {
        if (!is_int($prefixSize)) {
            throw new \Exception(__METHOD__.' requires first param, $prefixSize, to be an integer');
        }

        if ($prefixSize < 1 || $prefixSize > 31) {
            throw new \Exception(__METHOD__.' requires first param, $prefixSize, to be CIDR prefix size with value between 1 and 31 (inclusive)');
        }

        return new self(bindec(str_repeat('1', $prefixSize).str_repeat('0', 32 - $prefixSize)));
    }
938975
public function setField(AbstractFormField $field)
    {
        if (!($field instanceof UploadField)) {
            throw new \Exception('The validator "' . get_class($this) . '" only works on upload fields!');
        }

        $this->field = $field;
        return $this;
    }
793010
public function importField($field, $field_name = null)
    {
        $field = $this->model->hasElement($field);
        if (!$field) {
            return;
        }
        /** @type Field $field */
        if (!$field->editable()) {
            return;
        }

        if ($field_name === null) {
            $field_name = $this->_unique($this->owner->elements, $field->short_name);
        }
        $field_type = $this->getFieldType($field);
        $field_caption = $field->caption();

        $this->field_associations[$field_name] = $field;

        if ($field->listData() || $field instanceof Field_Reference) {
            if ($field_type == 'Line') {
                $field_type = 'DropDown';
            }
        }

        $form_field = $this->owner->addField($field_type, $field_name, $field_caption);
        $form_field->set($field->get());

        $field_placeholder = $field->placeholder() ?: $field->emptyText() ?: null;
        if ($field_placeholder) {
            $form_field->setAttr('placeholder', $field_placeholder);
        }

        if ($field->hint()) {
            $form_field->setFieldHint($field->hint());
        }

        if ($field->listData()) {
            /** @type Form_Field_ValueList $form_field */
            $a = $field->listData();
            $form_field->setValueList($a);
        }
        if ($msg = $field->mandatory()) {
            $form_field->validateNotNULL($msg);
        }

        if ($field instanceof Field_Reference || $field_type == 'reference') {
            $form_field->setModel($field->getModel());
        }
        if ($field->theModel) {
            $form_field->setModel($field->theModel);
        }
        if ($form_field instanceof Form_Field_ValueList && !$field->mandatory()) {
            /** @type string $text */
            $text = $field->emptyText();
            $form_field->setEmptyText($text);
        }

        if ($field->onField()) {
            call_user_func($field->onField(), $form_field);
        }

        return $form_field;
    }
115415
private function convertToCustomField($field)
    {
        if (!empty($field->attributes())) {
            if (isset($field->attributes()->{'field-name'}) && isset($field->attributes()->{'field-value'})) {
                $rawName = (string)$field->attributes()->{'field-name'};
                list($normalizedName, $prefix) = $this->splitFieldNameAndPrefix($rawName);
                $value = (string)$field->attributes()->{'field-value'};
                if (!empty($normalizedName)) {
                    return new CustomField($normalizedName, $value, $prefix);
                }
            }
        }
        return null;
    }
1185017
protected function makeFormFieldWidget($field)
    {
        if ($field->type !== 'widget') {
            return null;
        }

        if (isset($this->formWidgets[$field->fieldName])) {
            return $this->formWidgets[$field->fieldName];
        }

        $widgetConfig = $this->makeConfig($field->config);
        $widgetConfig->alias = $this->alias . studly_case(HtmlHelper::nameToId($field->fieldName));
        $widgetConfig->sessionKey = $this->getSessionKey();
        $widgetConfig->previewMode = $this->previewMode;
        $widgetConfig->model = $this->model;
        $widgetConfig->data = $this->data;
        $widgetConfig->parentForm = $this;

        $widgetName = $widgetConfig->widget;
        $widgetClass = $this->widgetManager->resolveFormWidget($widgetName);

        if (!class_exists($widgetClass)) {
            throw new ApplicationException(Lang::get(
                'backend::lang.widget.not_registered',
                ['name' => $widgetClass]
            ));
        }

        $widget = $this->makeFormWidget($widgetClass, $field, $widgetConfig);

        /*
         * If options config is defined, request options from the model.
         */
        if (isset($field->config['options'])) {
            $field->options(function () use ($field) {
                $fieldOptions = $field->config['options'];
                if ($fieldOptions === true) $fieldOptions = null;
                $fieldOptions = $this->getOptionsFromModel($field, $fieldOptions);
                return $fieldOptions;
            });
        }

        return $this->formWidgets[$field->fieldName] = $widget;
    }
87683
public function setFieldPrefixLength(string $field, int $prefixLength): self
    {
        return $this->setFieldParam($field, 'prefix_length', $prefixLength);
    }
602682
public static function createOrNull($data = null)
    {
        static $request;
        if (!$request) {
            $request = Di::getDefault()->getShared('request');
        }

        if (!$request->isPost()) {
            return null;
        }

        $instance = new static();

        $_request = $request->get();

        if ($data === null || !isset($data[0])) {
            foreach ($instance->getSafeFields() as $field) {
                if (isset($_request[$field])) {
                    $instance->$field = $_request[$field];
                }
            }
        }

        if ($data) {
            foreach ($data as $k => $v) {
                if (is_int($k)) {
                    $field = $v;
                    if (!isset($_request[$field])) {
                        throw new MissingFieldException($field);
                    }
                    $instance->$field = $_request[$field];
                } else {
                    $instance->$k = $v;
                }
            }
        }

        return $instance->create();
    }
613404
public function transformFormField(FormField $field)
    {
        $newfield = $field->performReadOnlyTransformation();

        $fieldname = $field->getName();
        if ($this->original->isLocalizedField($fieldname)) {
            $field->setName($this->original->getLocalizedFieldName($fieldname));
            $value = $this->original->getLocalizedValue($fieldname);
            if ($value instanceof DBField) {
                $field->setValue($value->getValue());
            } else {
                $field->setValue($value);
            }
        }

        return $this->baseTransform($newfield, $field, $fieldname);
    }
999617
public function add(FieldTypeInterface $field): FormBuilderInterface
    {
        /** @var BaseType $field */
        $opts = $this->validateOptions(array_merge([
            'errors' => $this->form->getOption('errors'),
            'theme' => $this->form->getOption('theme')
        ], $field->getOptions()), $field);
        $field->setLocale($this->form->getLocale());
        $field->setOptions($opts);
        $field->setForm($this->form);
        $field->setViewFactory($this->form->getViewer());
        $field->setResourceTransformerFactory($this->form->getResourceTransformerFactory());

        // DTO
        if (! is_null($this->dataClass) && is_object($this->dataClass) && $field->getOption('mapped')) {
            $this->dataMapperManager->mapFromObjectToField($this->dataClass, $field);
        }

        // Check if section instance already exists on the form.
        // If not, create a new section instance.
        if ($this->form->repository()->hasGroup($field->getOption('group'))) {
            // The section/group instance is already registered, just fetch it.
            $section = $this->form->repository()->getGroup($field->getOption('group'));
        } else {
            // No defined group. Let's create an instance so we can attach
            // the field to it right after.
            $section = new Section($field->getOption('group'));
        }

        // Setup group/section default view.
        $section->setTheme($this->form->getTheme());
        $section->setView('form.group');

        // Add the field first to section instance.
        // Then pass both objects to the repository.
        $section->addItem($field);
        $this->form->repository()->addField($field, $section);

        return $this;
    }
75444
protected function addField(Field $field)
    {
        if ($this->hasField($field->getName())) {
            throw new \Exception(
                'Field already defined for name: ' . $field->getName()
            );
        }

        $index = count($this->fields);

        $this->fieldsNameIndex[$index] = $field->getName();
        $this->fields[] = $field;
    }
1124936
public function addFieldCache($field)
    {
        if ( !$cache = $this->getMongator()->getFieldsCache() ) {
            return null;
        }

        $field = preg_replace('/\.\d+/', '', $field);

        foreach ($this->getQueryHashes() as $hash) {
            $value = $cache->get($hash);
            if (!$value) $value = array();

            if ( !isset($value['fields'][$field]) || $value['fields'][$field] != 1 ) {
                $value['fields'][$field] = 1;
                $cache->set($hash, $value);
            }
        }
    }
969982
private function customFieldInstance($prefix): array
    {
        $ns = $this->xmlns[$prefix];

        if (!$discoveredCustomField = $this->customFields[$ns]) {
            throw new LogicException(
                sprintf(
                    'No custom field class was found for %s. setCustomFields() must be called before using this extractor.',
                    $ns
                )
            );
        }

        return [new Type('object', false, $discoveredCustomField->getClass())];
    }
1226284
public function map($field, $context = null)
    {
        foreach ($this->mappers as $mapper) {
            $field = $mapper->map($field, $context);
        }
        return $field;
    }