2017-01-13 97 views
0

我如何驗證表格的角度?因爲我有下面的表格,其中使用https://github.com/danialfarid/ng-file-upload所有的領域是必需的。停止提交表格,除非填寫所有字段

問題是,當我點擊提交它顯示一個提示說該字段是必需的,但表單被提交。我想停止提交,除非填寫所有字段。

而我該如何讓用戶只從配置文件輸入中選擇圖像?按鈕時,雖然我加ng-accept="image/*"不工作

<form enctype="multipart/form-data" method="post" > 
<input type="text" ng-model="name" required> 
<input type="text" ng-model="age" required> 
<input type="text" ng-model="title" required> 
<input type="file" name="profile_pic" ng-file-select="onFileSelect" required> 
<input type="file" name="document" ng-file-select="onFileSelect2" required> 
<input type='button' ng-click='onFormSubmit(onFileSelect,onFileSelect)' value='Submit'> 
</form> 

angular.module('myApp', ['ngFileUpload']); 

var MyCtrl = [ '$scope', 'Upload', function($scope, Upload) { 
    $scope.onFileSelect = function(file) { 

Upload.upload({ 
    url: 'api/upload-data.php', 
    method: 'POST', 
    file: file, 
    data: { 
     'name': $scope.name, 
     'age' : $scope.age , 
     'title' : $scope.title , 


    } 
}) 

    }; 
}]; 
+1

爲表單指定一個名稱(比如說「myForm」),給出一個名稱到它的每個輸入,並使用'<按鈕ng-disabled =「myForm。$ invalid」>' –

回答

3

名稱添加到您的窗體(所以你可以參考它),改變你的堂妹NG-點擊NG-提交表單本身,如果表單無效,則將提交更改爲type =「submit」+ ng-disabled的按鈕元素

<form name="myForm" enctype="multipart/form-data" method="post" ng-submit="onFormSubmit(onFileSelect,onFileSelect)" > 
    <input type="text" ng-model="name" required> 
    <input type="text" ng-model="age" required> 
    <input type="text" ng-model="title" required> 
    <input type="file" name="profile_pic" ng-file-select="onFileSelect" required> 
    <input type="file" name="document" ng-file-select="onFileSelect2" required> 
    <button type="submit" ng-disabled="myForm.$invalid">Submit</button> 
</form> 
+0

ng-click on the button is fine,too。但按鈕應該是一個提交按鈕,因爲你正確地做你的答案。 –

+0

是的,如果您不想使用回車鍵提交表單,ng-click通常是可以的 - 但例如在登錄表單中,您希望啓用按enter鍵在輸入用戶名+密碼後提交表單,這將不起作用用ng-click編輯:Nevermind,ng-click很好! – Fissio

+0

對於ng-click也能正常工作。 https://docs.angularjs.org/api/ng/directive/form –

相關問題