Yii2 activeForm.js Validation



  • So there's a set of objects of the type files.

    [File, File, File]
    0:File
    1:File
    2:File
    

    How can you feed this to the Yii2 customer? Would you please share if anyone did this?



  • Validation is done with 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');
    }
    });

    Validation on server - Yii2 Uploading Multiple Files:

    First, you should build a model class by adding MAXFILES to the file verification rule to limit the maximum number of files allowed for downloading. MAXFILES 0 means that there is no restriction on the number of files that can be loaded simultaneously. The maximum number of files may be downloaded simultaneously with the PHP max_file_uploads, which is default 20. The download() method should also be updated to keep 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' =&gt; false, 'extensions' =&gt; 'png, jpg', 'maxFiles' =&gt; 4],
    ];
    

    }

    public function upload()
    {
        if ($this-&gt;validate()) {
            foreach ($this-&gt;imageFiles as $file) {
                $file-&gt;saveAs('uploads/' . $file-&gt;baseName . '.' . $file-&gt;extension);
            }
            return true;
        } else {
            return false;
        }
    }
    

    }

    In view, you should add multiple attributes to have 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 operation of the counteraller, you should cause ploadedFile::getInstances() instead of ploadedFile::getInstance() attach a copya UploadedFile to download Upload Form:

    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-&gt;request-&gt;isPost) {
            $model-&gt;imageFiles = UploadedFile::getInstances($model, 'imageFiles');
            if ($model-&gt;upload()) {
                // file is uploaded successfully
                return;
            }
        }
        return $this-&gt;render('upload', ['model' =&gt; $model]);
    }
    

    }

    http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html#uploading-multiple-files




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2