我如何获得 form->field() 值?

How can i get form->field() value?

简而言之,我有以下代码:

<?= $form->field( $isntmodel, 'id')->textInput() ?>
<?= Html::a('<i class="mdi-action-done"></i>', ['add-item', 'id' => ???], [
    'class' => 'btn-btn-add pull-right',
]) ?>

此代码错误。

所以。我需要获取用户输入的值。并将其设置为 ???

$form->field() 必须有 $model, $attribute, $options = []。如何不使用 $model 和 $attribute 来编写字段?它不是 table 列,我只需要获取值并将其设置为 ???

我试试

  public function actionAddItem($id) {
    $model = $this->$model;
    $product = Product::findOne($id);
    $orderItem = new OrderItem();
    $orderItem->order_id = $model->id;
    $orderItem->title = $product->title;
    $orderItem->price = $product->getPrice();
    $orderItem->product_id = $product->id;
    $orderItem->save();
    return $this->redirect(['index']);
  }

但是它抛出了一个异常。在 $model = $this->$model 行,我不知道如何从字段提交 id 到 link

如果我把它放在浏览器中,添加项目就可以了http://yii2-shop/backend/web/order/add-item?modelid=13&id=1&quantity=4

UPD

现在我的表单看起来像

<?php $form = ActiveForm::begin([]); ?>
    <tr>
        <td><?= $n ?></td>
        <td><?= $form->field( $model, 'newOrderItemId')->textInput()->label(false) ?></td>
        <td></td>
        <td></td>
        <td class="text-center"><?= $form->field( $model, 'newOrderItemQuantity')->textInput()->label(false) ?></td>
        <td>
            <?= Html::a('<i class="mdi-action-done"></i>', [
                '/order/add-item',
                'modelid' => $model->id,
                'id' => $model->newOrderItemId,
                'quantity' => $model->newOrderItemQuantity,
            ], [
              'class' => 'btn btn-add pull-right',
              'data-toggle'=>'tooltip' ,
              'data-placement'=>'bottom',
              'title'=>'Добавить товар',
            ]) ?>
        </td>
    </tr>
<?php ActiveForm::end(); ?>

而且add-item看起来像

public function actionAddItem($modelid, $id, $quantity) {
    $model = $this->findModel($modelid);
    $product = Product::findOne($id);
    $orderItem = new OrderItem();
    $orderItem->order_id = $model->id;
    $orderItem->title = $product->title;
    $orderItem->price = $product->getPrice();
    $orderItem->product_id = $product->id;
    $orderItem->quantity = $quantity;
    $orderItem->save();
    return $this->redirect(['index']);
}

newOrderItemIdnewOrderItemQuantity 只是我在 Order 模型中标记的 public 变量。我无法获取提交给 add-item

的表单字段值

所以。我解决了问题。

我为公告变量创建了 AddOrderItem 模型

<?php namespace backend\models;

use yii\base\Model;

class AddOrderItem extends Model {

  public $modelid;
  public $id;
  public $quantity;

  public function rules() {
    return [
      [['modelid','id','quantity'], 'integer'],
    ];
  }

}

然后我编辑了 actionUpdate() 现在看起来像

public function actionUpdate($id) {
    $model = $this->findModel($id);
    $addOrderModel = new AddOrderItem();
    if ($addOrderModel->load(Yii::$app->request->post())) {
      $product = Product::findOne($addOrderModel->id);
      $orderItem = new OrderItem();
      $orderItem->order_id = $model->id;
      $orderItem->title = $product->title;
      $orderItem->price = $product->getPrice();
      $orderItem->product_id = $product->id;
      $orderItem->quantity = $addOrderModel->quantity;
      $orderItem->save();
      return $this->redirect(['view', 'id' => $model->id]);
    }

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
      return $this->redirect(['view', 'id' => $model->id]);
    } else {
      return $this->render('update', [
        'model' => $model,
        'addOrderModel' => $addOrderModel
      ]);
    }
  }

views/order/update 我添加了以下行

<?= $this->render('_addItemForm', ['model' => $addOrderModel]); ?>

并且 _addItemForm 现在包含这个:

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin(); ?>

  <td><?= $form->field( $model , 'id')->textInput()->label(false) ?></td>

  <td></td>
  <td></td>

  <td class="text-center"><?= $form->field( $model , 'quantity')->textInput()->label(false) ?></td>

  <td>
    <?= Html::submitButton('<i class="mdi-action-done"></i>',[
      'class' => 'btn btn-add pull-right',
      'data-toggle'=>'tooltip' ,
      'data-placement'=>'bottom',
      'title'=>'Добавить товар',
    ]) ?>
  </td>

<?php ActiveForm::end(); ?>

我简直不敢相信这是我自己做的。我很高兴没有人可以提供帮助,因为现在我知道的更多了。