public function actionCreate()
{
$event_id = RegEvent::getEventId();
$number = RegEvent::setNumber();
$request = Yii::$app->request;
$model = new RegEvent(['number'=> $number, 'event_id' => $event_id]);
$models = [new RegEventUser()];
if (!isset($model, $models)) {
throw new NotFoundHttpException("The user was not found.");
}
$model->scenario = $model::SCENARIO_FRONTEND;
if ($request->isPost && $request->post('ajax') !== null) {
$data = Yii::$app->request->post('RegEventUser', []);
foreach (array_keys($data) as $index) {
$models[$index] = new RegEventUser(['reg_event_id' => $model->id]);
}
Model::loadMultiple($models, $request->post());
Yii::$app->response->format = Response::FORMAT_JSON;
$result = ActiveForm::validateMultiple($models);
return $result;
}
if ($model->load($request->post()) && $model->save() && Model::loadMultiple($models, $request->post())) {
return $this->redirect(['index']);
}
if (Yii::$app->request->isAjax){
return $this->renderAjax('_form', [
'model' => $model,
'models' => $models,
]);
}
return $this->render('create', [
'model' => $model,
'models' => $models,
]);
}
<?php $form = ActiveForm::begin([
//'enableAjaxValidation' => true,
//'enableClientValidation' => false,
'validateOnChange' => false,
'validateOnSubmit' => true,
'validateOnBlur' => false,
'options' => ['enctype' => 'multipart/form-data']
]); ?>
<?= $form->errorSummary($model)?>
<?= $form->field($model, 'rang_id')->widget(Select2::classname(), [
'data' => RegEventRule::getRangs(),
'options' => ['placeholder' => 'Выберите ранг ...'],
]);?>
<?= $form->field($model, 'discipline_id')->widget(Select2::classname(), [
'data' => RegEventRule::getDisciplines(),
'options' => ['placeholder' => 'Выберите дисциплину ...'],
]);?>
<?= $form->field($model, 'nomination_id')->widget(Select2::classname(), [
'data' => RegEventRule::getNominations(),
'options' => ['placeholder' => 'Выберите номинацию ...'],
]);?>
<?= $form->field($model, 'category_id')->widget(Select2::classname(), [
'data' => RegEventRule::getCategories(),
'options' => ['placeholder' => 'Выберите категорию ...'],
]);?>
<?= TabularInput::widget([
//'min' => 1,
'models' => $models,
'attributeOptions' => [
//'enableAjaxValidation' => true,
//'enableClientValidation' => false,
'validateOnChange' => false,
'validateOnSubmit' => true,
'validateOnBlur' => false,
],
'columns' => [
[
'name' => 'user_id',
'type' => Select2::className(),
'options' => [
'data' => Profile::getDancers(),
'options' => ['placeholder' => 'Выберите танцора ...'],
]
],
]
]); ?>
if ($model->load($request->post()) && $model->save() && Model::loadMultiple($models, $request->post())) {
foreach ($models as $mod) {
$new_model = new RegEventUser(); // модель связующей таблицы?
$new_model->reg_event_id = $model->id;
// заполняете ещё один атрибут
$new_model->save(false);
}
return $this->redirect(['index']);
}