hi everyone
i am new yii2 frame work
my error is
Exception (Unknown Property) ‘yii\base\UnknownPropertyException’ with message ‘Setting unknown property: yii\validators\FileValidator::types’
in my model
============
<?php
namespace app\models;
use Yii;
/**
*/
class Upload extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'upload';
}
/**
* @inheritdoc
*/
public function rules() {
return [
[['image'], 'safe'],
[['image'], 'file', 'types' => 'jpg'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'file_id' => 'File ID',
'image' => 'Image',
];
}
}
my view
=======
<?php
use yii\helpers\Html;
use kartik\form\ActiveForm;
use kartik\widgets\FileInput;
?>
<?php $form = ActiveForm::begin([‘options’ => [‘enctype’=>‘multipart/form-data’]]);?>
<?php echo $form->field($model, ‘image’)->widget(FileInput::classname(), [
'options' => ['accept' => 'image/*'],
]); ?>
<?php echo Html::submitButton(‘Submit’, [‘class’=>‘btn btn-primary’]);?>
<?php ActiveForm::end(); ?>
my controler
==============
<?php
namespace app\controllers;
use Yii;
use app\models\Upload;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
- UploadController implements the CRUD actions for Upload model.
*/
class UploadController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Upload models.
* @return mixed
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => Upload::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Upload model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Upload model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Upload();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->file_id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
public function actionUpload()
{
$model = new Upload;
if (!empty($_POST)) {
$model->image = $_POST['Upload']['image'];
$file = \yii\web\UploadedFile::getInstance($model, 'image');
var_dump($file);
// You can then do the following
if ($model->save()) {
$file->saveAs('path/to/file');
}
// its better if you relegate such a code to your model class
}
return $this->render('upload', ['model'=>$model]);
}
/**
* Updates an existing Upload model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->file_id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Upload model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Upload model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Upload the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Upload::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
what can i do for upload and save the file
please help me thanks in advance