Search code examples
yii2yii2-advanced-appyii2-modelyii2-validation

Disable ActiveForm Clientside Validation on button other than submit Yii2


  1. I have an upload excel to DB form.

  2. It has 1 File input and 2 buttons 'Upload' and 'Delete All'.

Problem Scenario:

I don't want client-side activeform validation on 'Delete All' as File is not required here.

Code:

<?php $form = ActiveForm::begin([ 'enableClientValidation' => false, 
            'options' =>['action' => Url::toRoute('/site/halltickets')
                        ,'method' => 'POST'
                        , 'enctype'=>'multipart/form-data']]); ?>

<?= $form->field($model, 'file')
    ->fileInput()->label("Upload Only Excel File [Allowed Formats: xlsx, xls]") ?>
<center>
    <?= Html::submitButton('Upload &raquo;', ['class' => 'btn btn-warning']) ?>
    <?= Html::a('Delete All &raquo;',
        Url::toRoute(['site/delete-halltickets']),
        ['class' => 'btn btn-danger',
            'data-confirm' => Yii::t('yii', 'Delete all Hall Ticket Allocations?'),
        ]);?>
</center>
<?php ActiveForm::end(); ?>

to understand better, below is the image.

enter image description here


Solution

  • It is bad idea to place "Delete All" button on the upload form. Insert this button outside the form. It will more user-friendly.

    Additionally you can remove "required" validator from model. Or define it only for the specific scenarios.