表单 – 如何获得form-> field()值?

简而言之,我有以下代码

<?= $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

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']);
}

newOrderItemId和newOrderItemQuantity只是我在Order模型中标记的公共变量.我无法获得表单字段值以将其提交到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',]) ?>
  </td>

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

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

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些