Saving data from modal form

Hi, i have new issue for saving data from some form i make inside modal, everything is looking good until i press save button but nothing going happen.

let me show you my code here…

This For Controller

public function actionInsert()
    {
		$this->layout = 'blank';
		
        $model = new ProgramDetail();
		$items = Packing::find()->all();
		$program_id = Yii::$app->request->get('program_id');
		$ambil = Program::findOne(['program_id'=>$program_id]);
		$batch = $ambil->program_batch;
		
		
        if ($this->request->isPost) {
			
			
            if ($model->load($this->request->post()) && $model->save()) {
				 Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
              return $this->redirect(['program/view', 'program_id' => $model->program_detail_program_id]);
            }
        } else {
            $model->loadDefaultValues();
        }
		

        return $this->renderajax('insert', [
           'model' => $model,
		   'items' => $items,
		   'program_id' => $program_id,
		   'batch' => $batch,
		  // 'program_batch' => $program_batch,
		
		]);
    }

from that code, there is an error for

 if ($model->load($this->request->post()) && $model->save())

Model not inserted due to validation error.

this for model code :

<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "program_detail".
 *
 * @property int $program_detail_id
 * @property int $program_detail_program_id
 * @property int $program_detail_program_batch
 * @property string $program_detail_pack
 * @property int $program_detail_pack_qty
 * @property string $program_detail_packdate
 * @property string $program_detail_shift
 * @property string|null $program_detail_operator
 * @property string|null $program_detail_description
 *
 * @property Program $programDetailProgram
 */
class ProgramDetail extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'program_detail';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['program_detail_program_id', 'program_detail_program_batch', 'program_detail_pack', 'program_detail_pack_qty', 'program_detail_packdate','program_detail_operator' , 'program_detail_shift'], 'required'],
            [['program_detail_program_id', 'program_detail_program_batch',  'program_detail_pack', 'program_detail_pack_qty'], 'integer'],
            [['program_detail_packdate'], 'safe'],
            [['program_detail_description'], 'string'],
            [['program_detail_pack'], 'string', 'max' => 20],
            [['program_detail_shift', 'program_detail_operator'], 'string', 'max' => 50],
            [['program_detail_program_id'], 'exist', 'skipOnError' => true, 'targetClass' => Program::class, 'targetAttribute' => ['program_detail_program_id' => 'program_id']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'program_detail_id' => 'Program Detail ID',
            'program_detail_program_id' => 'Program ID',
            'program_detail_program_batch' => 'Batch Program',
            'program_detail_pack' => 'Kemasan',
            'program_detail_pack_qty' => 'Kuantum Dus',
            'program_detail_packdate' => 'Tanggal Kemas',
            'program_detail_shift' => 'Shift',
            'program_detail_operator' => 'Operator',
            'program_detail_description' => 'Deskripsi',
        ];
    }

    /**
     * Gets query for [[ProgramDetailProgram]].
     *
     * @return \yii\db\ActiveQuery
     */
   /*  public function getProgramDetailProgram()
    {
        return $this->hasOne(Program::class, ['program_id' => 'program_detail_program_id']);
    } */
}

Thanks for help.

1 Like

Where do you get the error?

There are many places where an error can occur:

$this->request->isPost ... when it's not a Post, then it can fail
$model->load ... it may also fail
$model->save ... it calls $model->validate, where some error could occur

Use https://www.yiiframework.com/doc/api/2.0/yii-base-model#getErrors()-detail

You can debug the output to the log, you can print it:
https://www.yiiframework.com/doc/api/2.0/yii-baseyii#debug()-detail
https://www.yiiframework.com/doc/api/2.0/yii-helpers-vardumper#dump()-detail

From there you will know what errors you have.

From debuger info from this line i am get an error

if ($model->load($this->request->post()) && $model->save())

I’ll try sir…

You can try this at first:

if ($model->load($this->request->post()))

and see if the error is in this part. Then try this:

if ($model->load($this->request->post())) {
$model->validate();

to see the results of the validation.