[[+content_image]]
R
R
Roma Antonyuk2017-05-12 10:59:27
Yii
Roma Antonyuk, 2017-05-12 10:59:27

How to fix ajax validation of yii2 files?

There is ajax form validation.
my rules

[['passport_photos', 'oversea_passport_photos'], 'required', 'on' => 'vip'],
            [['oversea_passport_photos', 'passport_photos'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 10]

controller
$model = new Client();
        $model->scenario = 'vip';

        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }

and form
<?php $form = ActiveForm::begin([
            'action' => '/order/default/vip-form',
            'enableAjaxValidation' => true,
            'enableClientValidation' => false,
            'options' => [ 'enctype' => 'multipart/form-data']
        ]); ?>
        <?= $form->field($model, 'passport_photos[]' )->widget(FileInput::className(), [
            'options' => [
                'multiple' => true,
                'accept' => 'image/*'
            ],
            'pluginOptions' => [
                'allowedFileExtensions'=>[ 'jpg', 'png'],
                'uploadUrl' => 'false',
                'maxFileCount' => 10,
                'initialPreviewAsData' => true,
                'overwriteInitial' => false,
                'showRemove' => false,
                'showUpload' => false,
                'fileActionSettings' => [
                    'showUpload' => false
                ]
            ]
        ]) ?>
        <?= $form->field($model, 'oversea_passport_photos[]' )->widget(FileInput::className(), [
            'options' => [
                'multiple' => true,
                'accept' => 'image/*'
            ],
            'pluginOptions' => [
                'allowedFileExtensions'=>[ 'jpg', 'png'],
                'uploadUrl' => 'false',
                'maxFileCount' => 10,
                'initialPreviewAsData' => true,
                'overwriteInitial' => false,
                'showRemove' => false,
                'showUpload' => false,
                'fileActionSettings' => [
                    'showUpload' => false
                ]
            ]
        ]) ?>
        <?= Html::submitButton(Yii::t('layout', 'signup'), ['class' => 'btn btn-submit']) ?>
        <?php $form::end(); ?>

the problem is that these fields do not validate normally, how to fix it?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2017-05-12
@smilik96

the problem is that these fields do not validate normally, how to fix it?

What exactly is wrong? I forgot my magic ball at home.
Also check the request. If this is an ajax validation, then the file is hardly transferred there, so there is nothing to validate. Run a file check on the client, 98% of download widgets have this functionality out of the box. Well, you have to do in the model'skipOnEmpty' =>true

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question