wbraganca / yii2-dynamicform

It is widget to yii2 framework to clone form elements in a nested manner, maintaining accessibility.
Other
436 stars 438 forks source link

Cant save in data in database using dynamic form in one model #331

Open devansh-gtu opened 1 year ago

devansh-gtu commented 1 year ago

Controller: public function actionCreate() {

    $model = new UniversityCourseList;
    $Course= [new UniversityCourseList];
    $countries = ArrayHelper::map(Country::getAllCountries(), 'id', 'name');

    if ($this->request->isPost) 
    {
        if ($model->load($this->request->post()))
        {
            echo "hiii";
            die(); 
            $Course = Model::createMultiple(UniversityCourseList::classname());
            Model::loadMultiple($Course, Yii::$app->request->post());

        // validate all models
            $valid = $model->validate();
            $valid = Model::validateMultiple($Course) && $valid;

            if ($valid) 
            {
                $transaction = \Yii::$app->db->beginTransaction();

                try {
                    if ($flag = $model->save(false)) {
                        foreach ($Course as $Course) {
                            $Course->customer_id = $model->id;
                            if (!($flag = $Course->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                    }

                    if ($flag) {
                        $transaction->commit();
                        return $this->redirect('index');
                    }
                } catch (Exception $e) {
                    $transaction->rollBack();
                }
            }
        }
    }

    //  if ($model->load(Yii::$app->request->post())) 
    //  {

    //     $Course = Model::createMultiple(UniversityCourseList::classname());
    //     Model::loadMultiple($Course, Yii::$app->request->post());

    //     // validate all models
    //     $valid = $model->validate();
    //     $valid = Model::validateMultiple($Course) && $valid;

    //     if ($valid) 
    //     {
    //         $transaction = \Yii::$app->db->beginTransaction();

    //         try {
    //             if ($flag = $model->save(false)) {
    //                 foreach ($Course as $Course) {
    //                     $Course->customer_id = $model->id;
    //                     if (!($flag = $Course->save(false))) {
    //                         $transaction->rollBack();
    //                         break;
    //                     }
    //                 }
    //             }

    //             if ($flag) {
    //                 $transaction->commit();
    //                 return $this->redirect('index');
    //             }
    //         } catch (Exception $e) {
    //             $transaction->rollBack();
    //         }
    //     }
    // }

         return $this->render('create', [
            'model' => $model,
            'courseType' => $this->getOthers('course_type'),
            'languages' => $this->getOthers('languages'),
            'intake' => $this->getOthers('intake'),
            'durationType' =>$this->getOthers('duration_type'),             
            'university' => $this->getUniversityList(),
            'countries' => $countries,
            'degree' => Degree::getAllDegrees(),
            'majors' => Majors::getAllMajors(),  
            'degreeLevels' => DegreeLevel::getAllDegreeLevels(), 
            'Course' => (empty($Course)) ? [new UniversityCourseList] : $Course, 
        ]);
}

form: <?php

use yii\helpers\Html; use yii\widgets\ActiveForm; use common\models\University; use backend\models\UniversitySearch; use dosamigos\ckeditor\CKEditor; use yii\helpers\ArrayHelper; use wbraganca\dynamicform\DynamicFormWidget; use kartik\depdrop\DepDrop; use yii\helpers\Url; use yii\helpers\FileHelper; use kartik\file\FileInput; use kartik\select2\Select2; use kartik\date\DatePicker; use common\models\UniversityAdmission; use yii\web\JsExpression; use common\models\UniversityCourseList;

$this->registerJsFile('@web/js/programs.js'); $this->registerJsFile('@web/libs/select2/select2.full.min.js'); $this->registerCssFile('@web/libs/select2/select2.min.css'); $year=[0=>' ',1=>'Janurary',3=>'Feburary',3=>'March',4=>'April',5=>'May',6=>'June',7=>'July',8=>'August',9=>'September',10=>'October',11=>'November',12=>'December'];

?>

'dynamic-form']); ?>
'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_] 'widgetBody' => '.container-items', // required: css class selector 'widgetItem' => '.item', // required: css class 'min' => 1, // 0 or 1 (default 1) 'insertButton' => '.add-item', // css class 'deleteButton' => '.remove-item', // css class 'model' => $Course[0], 'formId' => 'dynamic-form', 'formFields' => [ 'name', 'university_id', 'degree_id', 'degree_level_id', 'intake', 'deadline1', 'intake2', 'deadline2', 'placement_fess', 'a_level', 'ib_level', 'fees_international_students', 'type', ], ]); ?>
$Course): ?>
Patner University:
isNewRecord) { echo Html::activeHiddenInput($Course, "[{$index}]id"); } ?>
field($Course, "[{$index}]name")->textInput(['maxlength' => true]) ?>
field($Course, "[{$index}]university_id")->dropDownList($university, ['prompt' => 'Select University']) ?>
field($Course, "[{$index}]degree_id")->dropDownList($degree, ['prompt' => 'Select Discipline','id'=>'degree_id']) ?>
field($Course, "[{$index}]degree_level_id")->dropDownList($degreeLevels, ['prompt' => 'Select Degree']) ?>
field($Course, "[{$index}]intake")->dropDownList($year)?>
field($Course, "[{$index}]deadline1")->widget(DatePicker::classname(), ['options' => ['placeholder' => 'Select date'],'pluginOptions' => ['format' => 'mm-dd','todayHighlight' => true]]);?>
field($Course, "[{$index}]intake2")->dropDownList($year) ?>
field($Course, "[{$index}]deadline2")->widget(DatePicker::classname(), ['options' => ['placeholder' => 'Select date'],'pluginOptions' => ['format' => 'mm-dd','todayHighlight' => true]]);?>
field($Course, "[{$index}]placement_fess")->textInput(['maxlength' => true]) ?>
field($Course, "[{$index}]a_level")->textInput(['maxlength' => true]) ?>
field($Course, "[{$index}]ib_level")->textInput(['maxlength' => true]) ?>
field($Course, "[{$index}]fees")->textInput(['maxlength' => true]) ?>
field($Course, "[{$index}]fees_international_students")->textInput(['maxlength' => true]) ?>
field($Course, "[{$index}]type")->dropDownList($courseType,['prompt' => 'Select Type']) ?>
isNewRecord ? 'Create' : 'Update', ['class' => 'btn btn-primary']) ?>