I get error courses must be an integer when I click on create after inputting the required values. Please, what could be the problem. I need someone to help me out. Below are my view and controller code, and also the attached error image.
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\Student;
use app\models\Faculty;
use app\models\Department;
use app\models\Course;
use yii\helpers\Url;
use kartik\select2\Select2;
/* @var $this yii\web\View */
/* @var $model app\models\Student */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="student-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'firstname')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'lastname')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'address')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'gender')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'email')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'phone')->textInput(['maxlength' => true]) ?>
<?php
$data = ArrayHelper::map(Faculty::find()->all(), 'id', 'facultyname');
echo $form->field($model, 'faculty_id')->widget(Select2::classname(), [
'data' => $data,
'language' => 'en',
'options' => ['prompt'=>'select faculty',
'placeholder' => 'Select Faculty.....',
'onChange' => '$.post("'.Url::to(['/department/lists']).'?id=' . '"+$(this).val(), function(data){
$("select#student-department_id").html(data);
});'
],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
<?php
echo $form->field($model, 'department_id')->widget(Select2::classname(), [
'language' => 'en',
'options' => ['prompt'=>'....Select Department....',
'placeholder' => 'Select Department...',
'onChange' => '$.post("'.Url::to(['course/lists']).'?id=' . '"+$(this).val(), function(data){
$("select#student-course_id").html(data);
});'
],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
<?php
echo $form->field($model, 'course_id')->widget(Select2::classname(), [
'data' =>[],
'language' => 'en',
'size' => Select2::MEDIUM,
'options' => ['prompt'=>'....Select Courses....','placeholder' => 'Select Courses...','multiple' => true,'selected' => 'selected'],
'pluginOptions' => ['allowClear' => true],
]);
?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
My controller
public function actionCreate()
{
$model = new Student();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}