public function actionIndex() { // TODO: implement ajax validation // TODO: modify this to automatic get class of dynamic model if (\Yii::$app->request->post('DynamicParam')) { $isUpdated = $isChanged = false; $models = Params::getDynamicModels(); DynamicParam::loadMultiple($models, $_POST); if (DynamicParam::validateMultiple($models)) { foreach ($models as $id => $model) { $owner = $model->owner; $owner->value = $model->{$owner->code}; $isChanged = $owner->isAttributeChanged('value'); $isUpdated = $isUpdated || $isChanged; if ($isChanged && !$owner->update(true, ['value'])) { // TODO: implement update fail } } } \Yii::$app->session->setFlash('yii2-params-updated', $isUpdated); // TODO: change flash key if (\Yii::$app->request->isAjax) { \Yii::$app->end(); } else { \Yii::$app->response->redirect(\Yii::$app->request->getReferrer()); } } else { \Yii::$app->response->redirect(\Yii::$app->request->getReferrer()); } }
public function run() { Pjax::begin(['enablePushState' => false, 'formSelector' => $this->getFormId()]); if (\Yii::$app->session->getFlash('yii2-params-updated')) { // TODO: review this custom alert code $closeButton = Html::button('×', ['data-dismiss' => 'alert', 'aria-hidden' => 'true', 'class' => 'close']); echo Html::tag('div', $closeButton . "Params updated successfully!", ['class' => 'alert-info alert fade in']); } /** @var \zarv1k\params\models\DynamicParam[] $models */ $models = \zarv1k\params\models\Params::getDynamicModels(); $form = ActiveForm::begin(['id' => $this->getFormId(), 'action' => \Yii::$app->getUrlManager()->createUrl("{$this->_moduleId}/manage")]); /** @var ActiveField $activeField */ $activeField = \Yii::$container->get('yii\\widgets\\ActiveField'); // TODO: review get from di foreach ($models as $model) { echo $form->field($model, "[{$model->owner->id}]{$model->owner->code}", ['labelOptions' => ArrayHelper::merge($activeField->labelOptions, ['label' => $model->owner->description, 'title' => $model->owner->name]), 'inputOptions' => ArrayHelper::merge($activeField->inputOptions, ['placeholder' => $model->owner->description, 'title' => $model->owner->name])]); } echo Html::submitButton($this->getSubmitContent(), $this->getSubmitOptions()); ActiveForm::end(); Pjax::end(); }