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.

    1 answer 1

    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

    • Thanks, about this all I know. The question is how to implement it using Yii itself, i.e. using activeForm.js And yes, you can screw something like github.com/jzaefferer/jquery-validation - Wanderer
    • echo $ form-> field ($ model, 'imageFiles []', ['enableClientValidation' => true]); - Vanya Avchyan
    • @ Wanderer I answered the question in which it was not said about this. It is necessary to formulate the question correctly. And it’s not lazy to describe. No one has to guess what you meant. - Vanya Avchyan
    • There after all it is told - validator Yii2 on the client. And yes, I do not need to poke, we did not move on with you. - Wanderer
    • @ Wanderer It is strange that from this just saw only 2 letters. Such as “you” should not be helped. Because you will always find something to be dissatisfied. Yes, it’s not typical for guys to be offended - Vanya Avchyan