In general, there is an array of file objects of the form
[File, File, File] 0:File 1:File 2:File
How can I feed this to the validator Yii2 on the client? Please share, if anyone did this.
In general, there is an array of file objects of the form
[File, File, File] 0:File 1:File 2:File
How can I feed this to the validator Yii2 on the client? Please share, if anyone did this.
Validation on the client is done using javascript.
var _validFileExtensions = ["jpg", "jpeg", "bmp", "gif", "png"]; $('input[type="file"]').on('change',function (e) { var files = e.target.files; for (var i = 0; i < files.length; i++) { var extansion = files[i].name.split('.')[1]; if (!extansion) alert('Validation fails'); if(!(extansion in _validFileExtensions)) alert('Validation fails'); } });
Server Validation - Yii2 Uploading Multiple Files:
First, you must configure the model class by adding the MAXFILES option in the file check rule to limit the maximum number of files allowed for download. Setting MAXFILES 0 means that there are no restrictions on the number of files that can be downloaded at the same time. The maximum number of files can be downloaded simultaneously is also limited with the PHP directive max_file_uploads, which defaults to 20. The download () method must also be updated to save the downloaded files one by one.
namespace app\models; use yii\base\Model; use yii\web\UploadedFile; class UploadForm extends Model { /** * @var UploadedFile[] */ public $imageFiles; public function rules() { return [ [['imageFiles'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4], ]; } public function upload() { if ($this->validate()) { foreach ($this->imageFiles as $file) { $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension); } return true; } else { return false; } } }
In the view, you must add the multiple attribute to be able to select multiple files.
<?php use yii\widgets\ActiveForm; ?> <?php $form = ActiveForm::begin(['enableClientValidation'=>true,'options' => ['enctype' => 'multipart/form-data']]) ?> <?= $form->field($model, 'imageFiles[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?> <button>Submit</button> <?php ActiveForm::end() ?>
And finally, in the controller action, you must call ploadedFile :: getInstances () instead of ploadedFile :: getInstance () to assign an array of the UploadedFile instance to upload UploadForm :: imageFiles
namespace app\controllers; use Yii; use yii\web\Controller; use app\models\UploadForm; use yii\web\UploadedFile; class SiteController extends Controller { public function actionUpload() { $model = new UploadForm(); if (Yii::$app->request->isPost) { $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles'); if ($model->upload()) { // file is uploaded successfully return; } } return $this->render('upload', ['model' => $model]); } }
http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html#uploading-multiple-files
Source: https://ru.stackoverflow.com/questions/543491/
All Articles