<?= $form->fIEld( $isntmodel,'ID')->textinput() ?><?= HTML::a('<i ></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 ><?= $form->fIEld( $model,'newOrderItemQuantity')->textinput()->label(false) ?></td> <td> <?= HTML::a('<i ></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现在包含:
<?PHPuse yii\helpers\HTML;use yii\Widgets\ActiveForm;$form = ActiveForm::begin(); ?> <td><?= $form->fIEld( $model,'ID')->textinput()->label(false) ?></td> <td></td> <td></td> <td ><?= $form->fIEld( $model,'quantity')->textinput()->label(false) ?></td> <td> <?= HTML::submitbutton('<i ></i>',[ 'class' => 'btn btn-add pull-right',]) ?> </td><?PHP ActiveForm::end(); ?>
我不敢相信我自己做了什么.我很高兴没有人帮忙,因为现在我知道的更多了.
总结以上是内存溢出为你收集整理的表单 – 如何获得form-> field()值?全部内容,希望文章能够帮你解决表单 – 如何获得form-> field()值?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)