yii2 kartik widget fileinput vs fileinput() function

1.4k views Asked by At

I want to upload files and replace and so on.When I use this in my view file:
echo $form->field($model, 'image')->fileInput();
everything works properly - the image is uploaded and the old one deleted. But not when I want to have more control:

echo $form->field($model, 'image')->widget(FileInput::className(), [
  'pluginOptions' => [
    'options'=>[
        'accept'=>'image/*',
        'multiple'=>true
    ],
    'allowedFileExtensions'=>['jpg','gif','jpeg', 'png'],
    'initialPreview'=>[
        Html::img($model->getImageUrl(), ['class'=>'file-preview-image', 'alt'=>$title, 'title'=>$title]),
    ],
    'showCaption' => false,
    'showRemove' => false,
    'showUpload' => false,
    'browseClass' => 'btn btn-grey',
    'browseIcon' => '<i class="fa fa-folder-open" style="font-weight: 600; line-height: 1.3em;"></i> ',
    'browseLabel' =>  'Profilbild'
  ],
])->label(false)

The last version do not work. What am I doing wrong in my active form?

Update1: adding the other files

This is the Controller

$model = $this->findModel($user_id, $mail_pre);
$oldFile = $model->getImageFile();
$oldAvatar = $model->avatar;
$oldFileName = $model->filename;
[...]
$image = $model->uploadImage();
if ($image === false) {
$model->avatar = $oldAvatar;
$model->filename = $oldFileName;
[...]
if ($image !== false && unlink($oldFile)) {
$path = $model->getImageFile();
$image->saveAs($path);
return $this->redirect([
[...]

And this is in the ActiveRecord:

public function uploadImage() {
$image = UploadedFile::getInstance($this, 'image');
if (empty($image)) {
    return false;
}
[...]

Here the rules in the record:

[['avatar', 'filename', 'image'], 'safe'],
[['image'], 'file', 'extensions'=>'jpg, gif, png', 'on'=>'update', 'message' => 'Wrong extension'],
0

There are 0 answers