简体   繁体   English

所需的验证不适用于输入类型文件

[英]required validation does not work with input type file

I am trying to apply the validation on file input type with the use of angular form and form properties like $valid, $invalid, $pristine, $dirty, $touched . 我正在尝试使用角度形式和形式属性(如$ valid,$ invalid,$ pristine,$ dirty,$ touched)对文件输入类型进行验证。

Note : All other validation except file type is working. 注意 :除文件类型外,所有其他验证均有效。

Code for html is like below: html的代码如下所示:

<form name="userForm" ng-submit="submitForm(userForm.$valid)" novalidate>
    ....
    ....
    ....
    <div class="form-group" ng-class="{ 'has-error' : userForm.email.$invalid && !userForm.email.$pristine }">
      <label>File</label>
      <input type="file" name="filetest" class="form-control" ng-model="user.file">
      <p ng-show="userForm.filetest.$invalid && !userForm.filetest.$touched" class="help-block">File is required.</p>
    </div>
    ....
    ....
    ....
</form>

but above snippet is not working. 但以上代码段无效。 Please help me get out of this issue. 请帮助我摆脱这个问题。

Here is plnkr link for the reference. 是plnkr链接供参考。

Your file input does not have required attribute. 您的文件输入没有必填属性。

EDIT: You also need to change condition in your ng-class : it should be 编辑:您还需要更改ng-class中的条件:应该是

'has-error' : userForm.filetest.$invalid && !userForm.filetest.$pristine 

instead of 代替

'has-error' : userForm.email.$invalid && !userForm.email.$pristine

The whole div element should look like this: 整个div元素应如下所示:

<div class="form-group" ng-class="{ 'has-error' : userForm.filetest.$invalid && !userForm.filetest.$pristine }">
    <label>File</label>
    <input type="file" name="filetest" class="form-control" ng-model="user.file" required>
    <p ng-show="userForm.filetest.$invalid && !userForm.filetest.$touched" class="help-block">File is required.</p>
</div>

I think it works as expected after changind the div as mentioned. 我认为在更改上述div后,它可以按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM