我使用了kartik的自定义文件输入它除了验证之外我的工作正常,这是我参加此扩展程序的参考站点 https://github.com/kartik-v/yii2-widget-fileinput 这是我的代码,
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
],
]);
现在,如果文件输入为空,我怎么验证呢?谁能告诉我这是什么解决方案?
答案 0 :(得分:1)
你可以在Yii2中使用AdHoc方法,在你的行动中试试这个:
/* @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);
}
答案 1 :(得分:0)
您只需在模型文件中添加验证,例如
public function rules()
{
return [
[['VideoURL'], 'required'],
];
}