简体   繁体   English

ng-file-upload验证未运行(表单始终有效)

[英]ng-file-upload validation does not run (form always valid)

I need a file-upload for every item in an array. 我需要为数组中的每个项目上传文件。 I'm using ng-repeat to iterate through the array and the ng-file-upload plugin to handle the file upload and validation. 我正在使用ng-repeat遍历数组和ng-file-upload插件来处理文件上传和验证。 The expected result would be multiple forms. 预期结果将是多种形式。 One for each iteration of ng-repeat 每次ng-repeat

But for some reason the validation never fails and the form is always submitted. 但是出于某种原因,验证永远不会失败,并且总是提交表单。

View code (simplified for clarity): 查看代码(为清楚起见已简化):

<div ng-repeat="point in endpoints">
<div class="panel-body" >
    <form name="uploadForm">
        <div class="media-uploader button drop-box"
            ng-model="file"
            name="file"
            ngf-drag-over-class="'dragover'"
            ngf-accept="{{point.endpoint.type.acceptedFormat.join('/*,') }}/*"
            ngf-select="uploadForm.$valid && submitUpload($file, point)"
            ngf-drop="submitUpload($file, point)"
            ngf-max-size="{{ maxFileUploadSize }}"
            ngf-pattern="{{ point.endpoint.type.acceptedFormat.join('/*,') }}/*"
            ngf-max-duration="{{ (point.timeslots * 15)+tolerance }}"
            ngf-validate = "{width: {min: {{ point.endpoint.format.width }}, max:{{ point.endpoint.format.width }}},
                            height: {min: {{ point.endpoint.format.height }}, max: {{ point.endpoint.format.height }}}
                            }">

            <p>Click or drag to select images and video</p>
        </div>
     </form>
     <div class="has-error" >
         <div ng-show="uploadForm.file.$error.maxHeight || uploadForm.file.$error.maxWidth ">
             Media must be {{ point.endpoint.format.width }} &times; {{ point.endpoint.format.height }}px
         </div>
         <div ng-show="uploadForm.file.$error.maxSize">
             Max upload size exceeded the maximum allowed size is 200MB
         </div>
         <div ng-show="uploadForm.file.$error.pattern ">
             This endpoint only allows {{ point.endpoint.type.acceptedFormat.join(', ') }} uploads
         </div>
         <div ng-show="uploadForm.file.$error.maxDuration ">
             Your video exceeds the maximum allowed time of {{ point.timeslots * 15 }} seconds.
         </div>
     </div>
 </div>

I suspect this has something to do with the scope of uploadForm within the ng-repeat , but if my understanding of ng-repeat is correct each iteration should have it's own scope. 我怀疑这与ng-repeatuploadForm范围有关,但是如果我对ng-repeat理解是正确的,则每次迭代都应具有自己的范围。

What am I missing? 我想念什么?

You can use ng-form to create dynamic forms and validate it 您可以使用ng-form创建动态表单并对其进行验证

    <form name="outerForm">
       <div ng-repeat="item in items">
       <ng-form name="innerForm">
          <input type="text" name="foo" ng-model="item.foo" />
          <span ng-show="innerForm.foo.$error.required">required</span>
       </ng-form>
       </div>
       <input type="submit" ng-disabled="outerForm.$invalid" />
    </form>

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

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