yii\widgets\ActiveForm::field PHP Method

field() public method

A form field is associated with a model and an attribute. It contains a label, an input and an error message and use them to interact with end users to collect their inputs for the attribute.
See also: fieldConfig
public field ( Model $model, string $attribute, array $options = [] ) : ActiveField
$model yii\base\Model the data model.
$attribute string the attribute name or expression. See [[Html::getAttributeName()]] for the format about attribute expression.
$options array the additional configurations for the field object. These are properties of [[ActiveField]] or a subclass, depending on the value of [[fieldClass]].
return ActiveField the created ActiveField object.
    public function field($model, $attribute, $options = [])
    {
        $config = $this->fieldConfig;
        if ($config instanceof \Closure) {
            $config = call_user_func($config, $model, $attribute);
        }
        if (!isset($config['class'])) {
            $config['class'] = $this->fieldClass;
        }
        return Yii::createObject(ArrayHelper::merge($config, $options, ['model' => $model, 'attribute' => $attribute, 'form' => $this]));
    }

Usage Example

Example #1
4
 /**
  * Формирование Html кода поля для вывода в форме
  * @param ActiveForm $form объект форма
  * @param array $options массив html атрибутов поля
  * @param bool|int $index инднкс модели при табличном вводе
  * @return string
  */
 public function renderInput(ActiveForm $form, array $options = [], $index = false)
 {
     $options = ArrayHelper::merge($this->options, $options);
     $attr = $this->modelField->attr;
     $defaults = ["maxFileSize" => $this->modelField->model->maxFileSize, "uploadRoute" => $this->defaultRoute];
     $widgetOptions = ArrayHelper::merge($defaults, $this->widgetOptions, ["options" => $options]);
     $attr = $this->getFormAttrName($index, $attr);
     return $form->field($this->modelField->model, $attr)->widget(Html5Widget::className(), $widgetOptions);
 }
All Usage Examples Of yii\widgets\ActiveForm::field