0

I used kartik's custom file input it is working fine for me except validation,this is reference site from where i taken this extension https://github.com/kartik-v/yii2-widget-fileinput Here is my code,

echo '<label class="control-label">Video</label>';
        echo FileInput::widget([
            'model' => $model,
            'attribute' => 'VideoURL',
            'options' => ['multiple' => false],
            'pluginOptions' => [
                'allowedFileExtensions'=>['mp4'],
                'showPreview' => false,
                'showRemove' => false,
                'showUpload' => false,
                'minFileCount' => 1
            ],
    ]);

Now how can i validate it if file input is blank ? Can anyone please tell me what is solution for this ?

Nikul
  • 465
  • 1
  • 8
  • 24
  • Are you looking for [skipOnEmpty](http://www.yiiframework.com/doc-2.0/guide-input-validation.html#handling-empty-inputs)? – Beowulfenator Nov 06 '15 at 22:24

2 Answers2

1

You can use AdHoc method in Yii2, In your action try this:

/* @var $validator \yii\validators\FileValidator */
/* @var $file  yii\validators\FileValidator */

$validator = new FileValidator(['extensions' => ['png','jpg']]); //set allowed file types and other file extensions

$file = UploadedFile::getInstanceByName('avatar'); // it should be your input name attribute

if( $validator->validate($file, $errors) ) { // Validating here
          //validation success
} else {
          print_r($error);            
}
Touqeer Shafi
  • 5,084
  • 3
  • 28
  • 45
0

you just add validation in your model file likee

public function rules()
{
   return [           
       [['VideoURL'], 'required'],
   ];
}
vishuB
  • 4,173
  • 5
  • 31
  • 49