Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alex290/yii2-dynamicform
https://github.com/alex290/yii2-dynamicform
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/alex290/yii2-dynamicform
- Owner: alex290
- Created: 2018-06-15T17:13:26.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-06-15T18:08:24.000Z (over 6 years ago)
- Last Synced: 2024-11-05T08:50:54.106Z (2 months ago)
- Language: JavaScript
- Size: 16.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
yii2-dynamicform
===================Это исправленная и дополненная копия из wbranganca и для поддержки некоторых наших проектов, которые используют это расширение
Installation
------------The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
php composer.phar require --prefer-dist alex290/yii2-dynamicform "*"
```or add
```
"alex290/yii2-dynamicform": "*"
```to the require section of your `composer.json` file.
Demos
-----* [Demo 1](http://wbraganca.com/yii2extensions/dynamicform-demo1/) - (Address Book).
* [Demo 2](http://wbraganca.com/yii2extensions/dynamicform-demo2/) - (File Upload).
* [Demo 3](http://wbraganca.com/yii2extensions/dynamicform-demo3/) - (Nested Dynamic Form).Usage
-----###The View
```php
'dynamic-form']); ?>
= $form->field($modelCustomer, 'first_name')->textInput(['maxlength' => true]) ?>
= $form->field($modelCustomer, 'last_name')->textInput(['maxlength' => true]) ?>
Addresses
'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 4, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelsAddress[0],
'formId' => 'dynamic-form',
'formFields' => [
'full_name',
'address_line1',
'address_line2',
'city',
'state',
'postal_code',
],
]); ?>
$modelAddress): ?>
Address
isNewRecord) {
echo Html::activeHiddenInput($modelAddress, "[{$i}]id");
}
?>
= $form->field($modelAddress, "[{$i}]full_name")->textInput(['maxlength' => true]) ?>
= $form->field($modelAddress, "[{$i}]address_line1")->textInput(['maxlength' => true]) ?>
= $form->field($modelAddress, "[{$i}]address_line2")->textInput(['maxlength' => true]) ?>
= $form->field($modelAddress, "[{$i}]city")->textInput(['maxlength' => true]) ?>
= $form->field($modelAddress, "[{$i}]state")->textInput(['maxlength' => true]) ?>
= $form->field($modelAddress, "[{$i}]postal_code")->textInput(['maxlength' => true]) ?>
= Html::submitButton($modelAddress->isNewRecord ? 'Create' : 'Update', ['class' => 'btn btn-primary']) ?>
```###Javascript Events
```javascript
$(".dynamicform_wrapper").on("beforeInsert", function(e, item) {
console.log("beforeInsert");
});$(".dynamicform_wrapper").on("afterInsert", function(e, item) {
console.log("afterInsert");
});$(".dynamicform_wrapper").on("beforeDelete", function(e, item) {
if (! confirm("Are you sure you want to delete this item?")) {
return false;
}
return true;
});$(".dynamicform_wrapper").on("afterDelete", function(e) {
console.log("Deleted item!");
});$(".dynamicform_wrapper").on("limitReached", function(e, item) {
alert("Limit reached");
});```
###The Controller (sample code)
```php
load(Yii::$app->request->post())) {$modelsAddress = DynamicModel::createMultiple(Address::classname());
DynamicModel::loadMultiple($modelsAddress, Yii::$app->request->post());// ajax validation
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge(
ActiveForm::validateMultiple($modelsAddress),
ActiveForm::validate($modelCustomer)
);
}// validate all models
$valid = $modelCustomer->validate();
$valid = DynamicModel::validateMultiple($modelsAddress) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $modelCustomer->save(false)) {
foreach ($modelsAddress as $modelAddress) {
$modelAddress->customer_id = $modelCustomer->id;
if (! ($flag = $modelAddress->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $modelCustomer->id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}return $this->render('create', [
'modelCustomer' => $modelCustomer,
'modelsAddress' => (empty($modelsAddress)) ? [new Address] : $modelsAddress
]);
}/**
* Updates an existing Customer model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$modelCustomer = $this->findModel($id);
$modelsAddress = $modelCustomer->addresses;if ($modelCustomer->load(Yii::$app->request->post())) {
$oldIDs = ArrayHelper::map($modelsAddress, 'id', 'id');
$modelsAddress = DynamicModel::createMultiple(Address::classname(), $modelsAddress);
DynamicModel::loadMultiple($modelsAddress, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsAddress, 'id', 'id')));// ajax validation
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge(
ActiveForm::validateMultiple($modelsAddress),
ActiveForm::validate($modelCustomer)
);
}// validate all models
$valid = $modelCustomer->validate();
$valid = DynamicModel::validateMultiple($modelsAddress) && $valid;if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $modelCustomer->save(false)) {
if (! empty($deletedIDs)) {
Address::deleteAll(['id' => $deletedIDs]);
}
foreach ($modelsAddress as $modelAddress) {
$modelAddress->customer_id = $modelCustomer->id;
if (! ($flag = $modelAddress->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $modelCustomer->id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}return $this->render('update', [
'modelCustomer' => $modelCustomer,
'modelsAddress' => (empty($modelsAddress)) ? [new Address] : $modelsAddress
]);
}...
}
```###To zero or more elements (use the following code in your view file)
```php
'dynamic-form']); ?>
= $form->field($modelCustomer, 'first_name')->textInput(['maxlength' => true]) ?>
= $form->field($modelCustomer, 'last_name')->textInput(['maxlength' => true]) ?>
'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 4, // the maximum times, an element can be added (default 999)
'min' => 0, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelsAddress[0],
'formId' => 'dynamic-form',
'formFields' => [
'full_name',
'address_line1',
'address_line2',
'city',
'state',
'postal_code',
],
]); ?>
Addresses
Add
$modelAddress): ?>
Address
isNewRecord) {
echo Html::activeHiddenInput($modelAddress, "[{$i}]id");
}
?>
= $form->field($modelAddress, "[{$i}]full_name")->textInput(['maxlength' => true]) ?>
= $form->field($modelAddress, "[{$i}]address_line1")->textInput(['maxlength' => true]) ?>
= $form->field($modelAddress, "[{$i}]address_line2")->textInput(['maxlength' => true]) ?>
= $form->field($modelAddress, "[{$i}]city")->textInput(['maxlength' => true]) ?>
= $form->field($modelAddress, "[{$i}]state")->textInput(['maxlength' => true]) ?>
= $form->field($modelAddress, "[{$i}]postal_code")->textInput(['maxlength' => true]) ?>
= Html::submitButton($modelAddress->isNewRecord ? 'Create' : 'Update', ['class' => 'btn btn-primary']) ?>
```