Cake\View\Helper\FormHelper::_initInputField PHP Method

_initInputField() protected method

Will also add the error class if the field contains validation errors. ### Options - secure - boolean whether or not the field should be added to the security fields. Disabling the field using the disabled option, will also omit the field from being part of the hashed key. - default - mixed - The value to use if there is no value in the form's context. - disabled - mixed - Either a boolean indicating disabled state, or the string in a numerically indexed value. - id - mixed - If true it will be auto generated based on field name. This method will convert a numerically indexed 'disabled' into an associative array value. FormHelper's internals expect associative options. The output of this function is a more complete set of input attributes that can be passed to a form widget to generate the actual input.
protected _initInputField ( string $field, array $options = [] ) : array
$field string Name of the field to initialize options for.
$options array Array of options to append options into.
return array Array of options for the input.
    protected function _initInputField($field, $options = [])
    {
        if (!isset($options['secure'])) {
            $options['secure'] = !empty($this->request->params['_Token']);
        }
        $context = $this->_getContext();
        if (isset($options['id']) && $options['id'] === true) {
            $options['id'] = $this->_domId($field);
        }
        $disabledIndex = array_search('disabled', $options, true);
        if (is_int($disabledIndex)) {
            unset($options[$disabledIndex]);
            $options['disabled'] = true;
        }
        if (!isset($options['name'])) {
            $endsWithBrackets = '';
            if (substr($field, -2) === '[]') {
                $field = substr($field, 0, -2);
                $endsWithBrackets = '[]';
            }
            $parts = explode('.', $field);
            $first = array_shift($parts);
            $options['name'] = $first . (!empty($parts) ? '[' . implode('][', $parts) . ']' : '') . $endsWithBrackets;
        }
        if (isset($options['value']) && !isset($options['val'])) {
            $options['val'] = $options['value'];
            unset($options['value']);
        }
        if (!isset($options['val'])) {
            $valOptions = ['default' => isset($options['default']) ? $options['default'] : null, 'schemaDefault' => isset($options['schemaDefault']) ? $options['schemaDefault'] : true];
            $options['val'] = $context->val($field, $valOptions);
        }
        if (!isset($options['val']) && isset($options['default'])) {
            $options['val'] = $options['default'];
        }
        unset($options['value'], $options['default']);
        if ($context->hasError($field)) {
            $options = $this->addClass($options, $this->_config['errorClass']);
        }
        $isDisabled = false;
        if (isset($options['disabled'])) {
            $isDisabled = $options['disabled'] === true || $options['disabled'] === 'disabled' || is_array($options['disabled']) && !empty($options['options']) && array_diff($options['options'], $options['disabled']) === [];
        }
        if ($isDisabled) {
            $options['secure'] = self::SECURE_SKIP;
        }
        if ($options['secure'] === self::SECURE_SKIP) {
            return $options;
        }
        if (!isset($options['required']) && empty($options['disabled']) && $context->isRequired($field)) {
            $options['required'] = true;
        }
        return $options;
    }