uploading multiple images using yii2 and kartik fileInput extension - php

I have this little problem in uploading multiple images using yii2 and Kartik fileInput extension.
this is the model:
public $file;
public static function tableName()
{
return 'news';
}
public function rules()
{
return [
[['news_desc', 'news_context', 'news_first_source', 'news_second_source', 'news_third_source', 'news_responsibe_party', 'news_first_testimony', 'news_second_testimony', 'news_body', 'news_lang'], 'string'],
[['news_gov_id', 'news_typ_id', 'news_is_in_slider', 'news_is_act', 'news_is_del'], 'integer'],
[['news_happened_date', 'news_created_date', 'file'], 'safe'],
[['news_typ_id', 'news_lang'], 'required'],
[['news_title'], 'string', 'max' => 255],
[['file'], 'file','maxFiles' => 6],
];
}
as you can notice I am using maxFiles but it didnt worked for me
Controller:
public function actionCreate_news()
{
$model = new News();
if ($model->load(Yii::$app->request->post())) {
$model->file = UploadedFile::getInstances($model, 'file');
var_dump($model->file);
die();}}
right now I am just var dump the files I get but the problem there is only one file not all the files i uploaded
view:
echo FileInput::widget([
'model' => $model,
'attribute' => 'file[]',
'name' => 'file[]',
'options' => [
'multiple' => 'true',
'accept' => 'image/*'
],
'pluginOptions' => [
'showCaption' => false,
'showRemove' => false,
'showUpload' => false,
'allowedFileExtensions' => ['jpg','jpeg','png'],
'overwriteInitial' => false
],
]);
i have read all things about this issue and tried all the possible solutions but the problem is still there
when i press submit for the form only the last file will be submited
thanks

Remove 'name' => 'file[]', from this widget also single quotes from true.
echo FileInput::widget([
'model' => $model,
'attribute' => 'file[]',
'options' => [
'multiple' => true,
'accept' => 'image/*'
],
'pluginOptions' => [
'showCaption' => false,
'showRemove' => false,
'showUpload' => false,
'allowedFileExtensions' => ['jpg','jpeg','png'],
'overwriteInitial' => false
],
]);
Original:
echo FileInput::widget([
'model' => $model,
'attribute' => 'attachment_1[]',
'options' => ['multiple' => true]
]);

Thank you all I figured out the solution for my question i was trying to upload the images when submitting the form but it turned that i was wrong so i used the widget to upload the images using ajax like in here:
http://webtips.krajee.com/ajax-based-file-uploads-using-fileinput-plugin/
then using the plugin event i took the names of files to save in the DB when the form submited

Related

Yii 2 Kartik input validation message is coming while clicking the browse button only

<?php use kartik\file\FileInput;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin([
'id' => 'import-pdf',
'options' => ['enctype' => 'multipart/form-data'],
]); ?>
<?=
$form->field($model, 'file_name')->widget(FileInput::classname(), [
'options' => ['multiple' => false],
'pluginOptions' => [
'showPreview' => false,
'showCaption' => true,
'showRemove' => true,
'showUpload' => false,
],
]);
?>
//file_name is the attribute I'm using it
public function rules()
{
return [
[['file_name'], 'required'],
[['status', 'total_pages', 'processed_pages', 'file_type'], 'safe'],
[['total_pages', 'processed_pages', 'file_type'], 'integer'],
[['file_name'], 'file', 'skipOnEmpty' => true, 'extensions' => 'pdf'],
[['status'], 'string', 'max' => 255],
];
}
File name cannot be blank message is coming while clicking browse button only, It should show the validation message after selecting the file only
https://i.stack.imgur.com/GuENh.png
Have you tried to assign to $file_name the uploadedFile instance before the validation?
$model->file_name = UploadedFile::getInstance($model, 'file_name');
or
$model->file_name = UploadedFile::getInstanceByName('nameOfTheField');

Kartik Select2 load ajax when other input change value

I need to load data with ajax to a Kartik Select2 but only when certain input changes values. Kind of like...
<?= $form->field($model, 'id_list')->widget(Select2::classname(), [
'data' => [],
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [
'placeholder' => 'List',
],
'pluginOptions' => [
'allowClear' => true,
'action' => '#input_first'.change // or something
'ajax' => [
'url' => Url::to(['/list']),
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
],
],
]);?>
Have you tried to use Select2 with DepDrop plugin (http://demos.krajee.com/widget-details/depdrop)?

yii2:upload is faild for file larger than 2 mb

i used kartik\file\FileInput for upload my file
my file is correct uploaded for file smaller than 2 mb !
what is my problem ????
echo '<label class="control-label">اضافه کردن فایل</label>';
echo FileInput::widget([
'model' => $model,
'pluginOptions' => [
'initialPreview' => [
"../../$url",
],
'initialPreviewAsData' => true,
'initialCaption' => "",
'initialPreviewConfig' => [
],
],
'attribute' => 'file',
'options' => ['multiple' => false,
'accept' => "$acc/*",
]
]);
}
this is my model :
class PostContent extends \yii\db\ActiveRecord
{
public $file;

yii2 kartik input widget not working properly

I added buttons for delete and drag but not working, my code in the view:
<?= $form->field($model, 'imageFiles[]')->widget(FileInput::classname(), [
'options' => ['multiple' => true, 'accept' => 'image/*',
'id'=>'inputFile'],
'pluginOptions' => [
'rtl'=>'true',
'fileActionSettings'=>['showZoom'=>true, 'showRemove' =>true,
'showDrag'=>true],
'previewFileType' => 'image',
'maxFileCount' => 4,
'showUpload' => false,
]
]);
Where are i making mistakes?
When using FORM submission mode (without uploadUrl). in this scenario you cannot REMOVE preview thumbnails before they are uploaded - ONE by ONE (you can only clear all - this is a native HTML FILE input limitation as one cannot edit the files in the input). in this case we must add uploadUrl:
<?= $form->field($model, 'imageFiles[]')->widget(FileInput::classname(), [
'options' => ['multiple' => true, 'accept' => 'image/*',
'id'=>'inputFile'],
'pluginOptions' => [
'uploadUrl' => '/site/index',
'rtl'=>'true',
'fileActionSettings'=>['showZoom'=>true, 'showRemove' =>true,
'showDrag'=>true],
'previewFileType' => 'image',
'maxFileCount' => 4,
'showUpload' => false,
]
]);
This is ajax submission mode with uploadUrl.

Yii2 kartik datepicker multiple inline

I want a Datepicker inline with multidate enabled.
It is rendered correctly but returns only the last selected date.
Return value: '12.11.2016'
If I set 'type' to TYPE_INPUT everything is working fine.
Expected return Value: '23.11.2016, 24.11.2016, 18.11.2016, 12.11.2016'
<?= $form->field($model, 'dateString')->widget(DatePicker::className(), [
'type' => DatePicker::TYPE_INLINE,
'pluginOptions' => [
'multidate' => true,
],
]);
?>
I'm using "kartik-v/yii2-widget-datepicker": "#dev" and the model is a ActiveRecord Model.
Seems to have been fixed with this commit: https://github.com/kartik-v/yii2-widget-datepicker/commit/39e0e71277d0f115341e118a2b879a0dfcbd01c3
This will solve the problem
echo $form->field($model, 'date_time')->widget(DatePicker::classname(), [
'options' => [
'value' => "Jul-11-2020,Jul-14-2021,Jul-12-2020",
'class' => "col-md-12 form-control picker"
],
'readonly' => false,
'language' => 'en',
'type' => DatePicker::TYPE_INLINE,
'pluginOptions' => [
'format' => 'M-dd-yyyy',
'todayHighlight' => true,
'multidate' => true,
],
]);

Categories