I am using kartik file input widget in a yii2 project.
http://demos.krajee.com/widget-details/fileinput
Everything is fine until the update page is accessed where the already uploaded images for a particular post are displayed using the 'initialPreview' option. The preview works, but when the form is submitted, the file input is empty and the form won't validate. Poster Image (256x376) cannot be blank.
<?php
echo $form->field($model, 'posterImage')->widget(FileInput::classname(), [
'options' => [
'multiple' => true,
'accept' => 'image/*',
],
'pluginOptions' => [
'maxImageWidth' => 265,
'maxImageHeight' => 376,
'minImageWidth' => 265,
'minImageHeight' => 376,
'previewFileType' => 'image',
'allowedFileExtensions' => [
'jpg', 'jpeg'
],
'showUpload' => false,
'maxFileSize' => 200,
'maxFileCount' => 1,
'initialPreview' => [
$model->posterImage ? Html::img($model->posterImage, ['width' => '100%']) : NULL,
],
'initialPreviewConfig' => [
['url' => $model->posterImage],
],
'initialPreviewAsData' => false,
'overwriteInitial' => true,
]
]);
?>
You can use skipOnEmpty
on yii2 model
rules with update
scenarios
public function rules()
{
return [
[['posterImage'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg'],
];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With