如何在Yii2的索引页上添加输入字段

问题描述 投票:0回答:1

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

 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()


  ?>

刷新页面时,我会得到

类yii \ widgets \ ActiveForm的对象无法转换为字符串

我该如何实现?

任何帮助将不胜感激

yii2 yii2-advanced-app yii2-model
1个回答
0
投票

您的错误是<?=,它正在尝试输出整个脚本,而您无法回显$formActiveForm::begin()

将其更改为

<?php 

  $form = ActiveForm::begin();
  $relModel=$model->getModels()[0]; print_r($relModel['cust_id']);
  echo $form->field($relModel, 'cust_id')->textInput()
?>
© www.soinside.com 2019 - 2024. All rights reserved.