我想我被困了。所以我问这里..
我使用了Yii2,这是我项目的基本模板。
一切都很好,直到我想验证模型。
这是我的模特:
<?php
namespace app\models;
use yii\web\UploadedFile;
class KTPForm extends \yii\base\Model {
public $nik;
public $ktpLama;
public $copyKK;
public $resiDesa;
public $izazah_raport;
public $suratPindah;
public $suratHilang;
public function rules() {
return [
['nik', 'required'],
[['ktpLama,copyKK,resiDesa,izazah_raport,suratPindah,suratHilang'], 'file', 'extensions' => 'gif, jpg, png, jpeg'],
];
}
public function saveImages() {
$model = new KTPForm();
$model->ktpLama = UploadedFile::getInstance($model, 'ktpLama');
$model->copyKK = UploadedFile::getInstance($model, 'copyKK');
$model->resiDesa = UploadedFile::getInstance($model, 'resiDesa');
$model->izazah_raport = UploadedFile::getInstance($model, 'izazah_raport');
$model->suratPindah = UploadedFile::getInstance($model, 'suratPindah');
$model->suratHilang = UploadedFile::getInstance($model, 'suratHilang');
$currdate = date("Y-m-d");
$ktp = "";
$kk = "";
$resi = "";
$izazah = "";
$pindah = "";
$hilang = "";
if ($model->validate()) {
$ktp = $model->nik . '_' . $currdate . '_ktp.' . $model->ktpLama->extension;
$model->ktpLama->saveAs('upload/' . $ktp);
$kk = $model->nik . '_' . $currdate . '_kk.' . $model->ktpLama->extension;
$model->ktpLama->saveAs('upload/' . $kk);
$resi = $model->nik . '_' . $currdate . '_resi.' . $model->ktpLama->extension;
$model->ktpLama->saveAs('upload/' . $resi);
$izazah = $model->nik . '_' . $currdate . '_izazah.' . $model->ktpLama->extension;
$model->ktpLama->saveAs('upload/' . $izazah);
$pindah = $model->nik . '_' . $currdate . '_pindah.' . $model->ktpLama->extension;
$model->ktpLama->saveAs('upload/' . $pindah);
$hilang = $model->nik . '_' . $currdate . '_hilang.' . $model->ktpLama->extension;
$model->ktpLama->saveAs('upload/' . $hilang);
return true;
}
return false;
}
}
这是我的控制者:
public function actionKtp() {
if (Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new \app\models\KTPForm();
if ($model->load(Yii::$app->request->post())) {
if ($model->saveImages()) {
echo "God";
} else {
print_r($model->getErrors());
}
} else {
return $this->render('_ktp', [
'model' => $model,
]);
}
}
这就是我得到的。
请有人帮我.. :)
答案 0 :(得分:1)
在rules()数组中列出属性时,它们应该列为字符串数组,如下所示:
public function rules() {
return [
['nik', 'required'],
[['ktpLama','copyKK','resiDesa','izazah_raport','suratPindah','suratHilang'], 'file', 'extensions' => 'gif, jpg, png, jpeg'],
];
}
不是一个字符串:'ktpLama,copyKK,resiDesa,izazah_raport,suratPindah,suratHilang'