1 | initial version |
To incorporate a button for adding a product in Yii2 ActiveForm, you can follow these steps:
Create a form in Yii2 ActiveForm that includes the fields for product details (such as name, price, description, etc.).
Add a "Add Product" button to the form using the following code:
<?= Html::submitButton('Add Product', ['class' => 'btn btn-primary']) ?>
This will create a button with the label "Add Product" and the Bootstrap CSS class "btn btn-primary".
public function actionAddProduct()
{
$model = new Product();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
// product saved successfully
return $this->redirect(['view', 'id' => $model->id]);
}
// display the form
return $this->render('add-product', [
'model' => $model,
]);
}
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$this->title = 'Add Product';
?>
<h1><?= Html::encode($this->title) ?></h1>
<div class="product-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'price')->textInput() ?>
<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>
<div class="form-group">
<?= Html::submitButton('Add Product', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?= Html::a('Add Product', ['add-product'], ['class' => 'btn btn-success']) ?>
This will create a link or button with the label "Add Product", the Bootstrap CSS class "btn btn-success", and a URL to the "Add Product" action in the controller.
With these steps, you can incorporate a button for adding a product in Yii2 ActiveForm.