Image update in Yii2

When updating an image using Yii2, I face a validation problem. He always asks me to upload an image. But I do not want this. Always updating the image is not required.

I tried skipOnEmpty , but it does not work properly, it causes an effect when loading a photo, which is also incorrect.

Please, help!

Model

 public function rules() { return [ [['carid', 'name'], 'required'], [['carid', 'coverphoto', 'status'], 'integer'], [['name'], 'string', 'max' => 200], [['imageFiles'], 'image','extensions' => 'png, jpg, jpeg, gif', 'maxFiles' => 4, 'minWidth' => 100, 'maxWidth' => 800, 'minHeight' => 100, 'maxHeight'=>600,'skipOnEmpty' => true], ]; } 

controller

 public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->photoid]); } else { return $this->render('update', [ 'model' => $model, ]); } } 
+7
yii2
source share
3 answers

You must use scenario to update.

How, Add an on clause to a model rule to apply scenario .

  [['imageFiles'], 'image','extensions' => 'png, jpg, jpeg, gif', 'maxFiles' => 4, 'minWidth' => 100, 'maxWidth' => 800, 'minHeight' => 100, 'maxHeight'=>600,'skipOnEmpty' => true, 'on' => 'update-photo-upload'], 

And use this scenario in the controller action.

 public function actionUpdate($id) { $model = $this->findModel($id); $model->scenario = 'update-photo-upload'; ........ ..... } 
+4
source share

try the rule like

 [ ['imageFiles'], 'file', 'extensions' => 'png, jpg, jpeg, gif', 'mimeTypes' => 'image/jpeg, image/png', 'maxFiles' => 4, 'minWidth' => 100, 'maxWidth' => 800, 'minHeight' => 100, 'maxHeight'=>600, 'skipOnEmpty' => true ], 

This works in my case, hope it works for you too.

0
source share
  **Your View Like This** <?php use yii\widgets\ActiveForm;?> <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?> <?= $form->field($model, 'imageFiles')->fileInput() ?> <div class="form-group"> <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> </div> <?php ActiveForm::end() ?> *** Your Controller Like This****** use Yii; use yii\web\Controller; use app\models\UploadForm; use yii\web\UploadedFile; class SiteController extends Controller { public function actionUpdate() { $model = new UploadForm (); $model->scenario = 'update'; if (Yii::$app->request->isPost) { $model->imageFiles= UploadedFile::getInstance($model, 'imageFiles'); if ($model->upload()) { // file is uploaded successfully return; } } return $this->render('update', ['model' => $model]); } } ***** Your ModelLike This ****** use yii\base\Model; use yii\web\UploadedFile; class UploadForm extends Model { /** * @var UploadedFile[] */ public $imageFiles; public function rules() { return [ [['carid', 'name'], 'required'], [['carid', 'coverphoto', 'status'], 'integer'], [['name'], 'string', 'max' => 200], [['imageFiles'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4,'on'=>'update'], ]; } public function upload() { if ($this->validate()) { foreach ($this->imageFiles as $file) { $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension); } return true; } else { return false; } } function scenario() { return [ 'create' => ['imageFiles ', 'carid','name','coverphoto','status'], 'update' => ['imageFiles ', 'carid','name','coverphoto','status'], ]; } } 
0
source share

All Articles