Yii2 如何在索引页添加输入框

How to add an input field on the index page in Yii2

我正在用 yii2 做一个项目。我需要在 index 页面上放置一个 input field。该字段应类似于表单字段。即创建记录时在表单中使用的字段类型。为此,我做了以下工作

 public function actionIndex()
{
    $model = MdcmetercustRel::className();// this is the class whose data field I want to get
    $searchModel = new MdcmetersdataSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'model' => $model,

    ]);
}

我的指数

 <?=

  $form = ActiveForm::begin();
  $relModel=$model->getModels()[0]; print_r($relModel['cust_id']);
   $form->field($relModel, 'cust_id')->textInput()


  ?>

当我刷新我的页面时,我得到

Object of class yii\widgets\ActiveForm could not be converted to string

我怎样才能做到这一点?

任何帮助将不胜感激

你的错误是 <?= 它试图输出整个脚本,你不能回显 $formActiveForm::begin()

改为

<?php 

  $form = ActiveForm::begin();
  $relModel=$model->getModels()[0]; print_r($relModel['cust_id']);
  echo $form->field($relModel, 'cust_id')->textInput()
?>

您还需要更改

$model = MdcmetercustRel::className();

$model = new MdcmetercustRel();

否则 $model 中不会有对象,而是字符串,即 class 命名空间。