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

标签 forms field yii2

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

<?= $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 编写字段?它不是表格列,我只需要获取值并设置它???
我试试
  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 到链接

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

现在我的表格看起来像
<?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(); ?>

我不敢相信我自己做了什么。我很高兴没有人可以帮助,因为现在我知道的更多了。

关于forms - 我如何获得 form->field() 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30261415/

相关文章:

forms - NgModule 和 FormModule 在每个模块上导入?

django - 表单错误 - __init__() 获得意外的关键字参数 'prefix'

r - 在ggplot2中的等高线的平坦部分上绘制标签

tfs - 如何禁止基于另一个字段的值在 TFS 要求工作项中将状态从建议更改为事件?

javascript - 将 setInterval 函数时间保存在 yii2 的配置文件中

layout - Yii2 如何将模型实例传递到主布局?

php - Yii2 rest api getBodyParams() 无法从 PUT 请求中获取参数

javascript - JQuery 验证插件 : How do I get it to revalidate

javascript - 根据所选值传递隐藏输入

mysql - 错误 CONCAT MySQL - 没有连接 2 个或更多值