I am very new to Yii.
I have to apply image validation (.svg and .png allowed) only if user have selected the image.
public function rules() {
return [
[['logo'], 'file', 'extensions'=>'svg, png'],
];
}
When user select image it works fine. But on update form we have only name of file. Now if we submit the form it will apply validation. I need validation only if user changes the image.
My Controller code
if ($model->load(Yii::$app->request->post())) {
$model->logo = UploadedFile::getInstance($model, 'logo');
if (!empty($model->logo)) {
$model->logo->name = $model->logo->baseName . Yii::$app->formatter->asTimestamp(date('Y-d-m h:i:s')) . '.' . $model->logo->extension;
$logoPath = Yii::getAlias('@common') . '/web/uploads/logo/' . $model->logo->name;
$model->logo->saveAs($logoPath, false);
}
if ($model->updateSite()) {
return $this->redirect(['site-list']);
}
}
Please let me know if you need more clarification. Thanks.