Yii 2 Kartik input validation message is coming while clicking the browse button only

253 views Asked by At
  <?php     use kartik\file\FileInput;
              use yii\widgets\ActiveForm;

              $form = ActiveForm::begin([
             'id' => 'import-pdf',
             'options' => ['enctype' => 'multipart/form-data'],
    ]); ?>

        <?=
        $form->field($model, 'file_name')->widget(FileInput::classname(), [
            'options' => ['multiple' => false],
            'pluginOptions' => [
                'showPreview' => false,
                'showCaption' => true,
                'showRemove' => true,
                'showUpload' => false,
            ],
        ]);
        ?>
//file_name is the attribute I'm using it

    public function rules()
   {
       return [
           [['file_name'], 'required'],
           [['status', 'total_pages', 'processed_pages', 'file_type'], 'safe'],
           [['total_pages', 'processed_pages', 'file_type'], 'integer'],
           [['file_name'], 'file', 'skipOnEmpty' => true, 'extensions' => 'pdf'],
           [['status'], 'string', 'max' => 255],
       ];
   }

File name cannot be blank message is coming while clicking browse button only, It should show the validation message after selecting the file only

https://i.stack.imgur.com/GuENh.png

1

There are 1 answers

1
ilbassa On

Have you tried to assign to $file_name the uploadedFile instance before the validation?

$model->file_name = UploadedFile::getInstance($model, 'file_name');

or

 $model->file_name = UploadedFile::getInstanceByName('nameOfTheField');