PHP code example of pavlinter / yii2-multifields
1. Go to this page and download the library: Download pavlinter/yii2-multifields library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pavlinter / yii2-multifields example snippets
php composer.phar
use pavlinter\multifields\ModelHelper;
use yii\web\Response;
...
public function actionCreate()
{
$model = new Product();
$options = [new ProductOption(['scenario' => 'multiFields'])];
if(Yii::$app->request->isPost) {
$loaded = $model->load(Yii::$app->request->post());
$loaded = ModelHelper::load($options) && $loaded;
if ($loaded) {
if (ModelHelper::validate([$model, $options])) {
$model->save(false);
$newId = [];
foreach ($options as $oldId => $option) {
$option->id_product = $model->id;
$option->save(false);
ModelHelper::ajaxChangeField($newId, $option, 'name', $oldId);
ModelHelper::ajaxChangeField($newId, $option, 'value', $oldId);
}
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ['r' => 1, 'newId' => $newId];
} else {
return $this->redirect(['index']);
}
} else {
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
$errors = ModelHelper::ajaxErrors([$model, $options]);
return ['r' => 0, 'errors' => $errors];
}
}
}
}
return $this->render('create', [
'model' => $model,
'options' => $options,
]);
}
public function actionUpdate($id)
{
$model = Product::findOne($id);
$options = ProductOption::find()->where(['id_product' => $id])->indexBy('id')->all();
if (empty($options)) {
$options[] = new ProductOption(['scenario' => 'multiFields']);
} else {
foreach ($options as $option) {
$option->scenario = 'multiFields';
}
}
if(Yii::$app->request->isPost) {
$loaded = $model->load(Yii::$app->request->post());
$loaded = ModelHelper::load($options) && $loaded;
if ($loaded) {
if (ModelHelper::validate([$model, $options])) {
$model->save(false);
$newId = [];
foreach ($options as $oldId => $option) {
$option->id_product = $model->id;
$option->save(false);
ModelHelper::ajaxChangeField($newId, $option, 'name', $oldId);
ModelHelper::ajaxChangeField($newId, $option, 'value', $oldId);
}
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ['r' => 1, 'newId' => $newId];
} else {
return $this->redirect(['index']);
}
} else {
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
$errors = ModelHelper::ajaxErrors([$model, $options]);
return ['r' => 0, 'errors' => $errors];
}
}
}
}
return $this->render('update', [
'model' => $model,
'options' => $options,
]);
}
public function actionDeleteOption()
{
$id = Yii::$app->request->post('id');
$model = ProductOption::findOne($id);
if ($model !== null) {
$model->delete();
}
Yii::$app->response->format = Response::FORMAT_JSON;
return ['r' => 1];
}
use pavlinter\multifields\MultiFields;
...
$this->registerJs('
$("#product-form").on("beforeSubmit",function(e){
var $form = $(this);
$.ajax({
url: $form.attr("action"),
type: "POST",
dataType: "json",
data: $form.serialize(),
}).done(function(d){
if(d.r) {
$form.trigger("updateRows",[d.newId]).trigger("scrollToTop");
} else {
$form.trigger("updateErrors",[d.errors]).trigger("scrollToError");
}
});
return false;
});
');