简体   繁体   English

在关闭之前验证ngDialog openConfirm中的表单

[英]Validate form within ngDialog openConfirm before closing

I have a button that opens an ngDialog.openConfirm . 我有一个打开ngDialog.openConfirm的按钮。 Within that dialog I have a form, which includes a textarea which is required and needs to be a minimum 20 characters. 在该对话框中,我有一个表单,其中包含一个文本区域,该区域是必需的,并且至少必须包含20个字符。

Here is a simplified version of my code: 这是我的代码的简化版本:

someFunction() {
    let newScope = $scope.$new();
    newScope.vm = vm;
    ngDialog.openConfirm({
        scope: newScope,
        template: 'componentDescription.html'
    })
}

And my html: 和我的HTML:

<form role="form" name="subForm">
   <textarea name="compDesc"
             required="true"
             ng-minlength="20"
             ng-model="vm.compDesc">
   </textarea>
   <button type="button" ng-click="confirm(0)">Submit</button>
   <button type="button" ng-click="closeThisDialog(0)">Cancel</button>
</form>

I would like for the Dialog to only be submitted if the form is valid. 我希望仅在表单有效时才提交对话框。

I tried to do this by creating a function in my controller , but that has trouble accessing the form/closing the dialog. 我试图通过在控制器中创建一个函数来做到这一点,但是在访问表单/关闭对话框时遇到了麻烦。

Any ideas? 有任何想法吗?

UPDATE: I've changed my html like so: 更新:我已经像这样更改了我的html:

<form role="form" name="subForm" novalidate ng-submit="confirm(0)">
   <textarea name="compDesc"
             required="true"
             ng-minlength="20"
             ng-model="vm.compDesc">
   </textarea>
   <button type="submit">Submit</button>
   <button type="button" ng-click="closeThisDialog(0)">Cancel</button>
</form>

This works on the first click, which brings up my error messages, but on the second click it submits the form even though it's invalid. 这在第一次单击时起作用,这会显示我的错误消息,但是在第二次单击时,即使它无效,它也会提交表单。 Seems that whenever the error messages are displayed the submit button ignores the validation. 似乎无论何时显示错误消息,提交按钮都会忽略验证。

只需您可以使用表单名称添加ng-disabled选项

<button type="button" ng-disabled="subForm.$invalid" ng-click="confirm(0)">Submit</button>

you can disable submit button if length of input string is less then 20 如果输入字符串的长度小于20,则可以禁用提交按钮

<button type="button" ng-disabled="vm.compDesc.length < 20" ng-click="confirm(0)">Submit</button>

you can also display an error message below text area so users can understand why submit button is not enabled 您还可以在文本区域下方显示错误消息,以便用户了解为什么未启用“提交”按钮

<form role="form" name="subForm">
   <textarea name="compDesc"
             required="true"
             ng-minlength="20"
             ng-model="vm.compDesc">
   </textarea>
   <p ng-show="vm.compDesc.length < 20" style="color:#cc5965">Description should be at least 20 characters</p> 
   <button type="button" ng-click="confirm(0)">Submit</button>
   <button type="button" ng-click="closeThisDialog(0)">Cancel</button>
</form>

You can manually decide whether to call $scope.confirm() or not, 您可以手动决定是否调用$ scope.confirm(),

Pass validation then call $scope.confirm(). 通过验证,然后调用$ scope.confirm()。

Not Pass validation, don't call $scope.confirm(). 不通过验证,请勿调用$ scope.confirm()。

eg 例如

Template: 模板:

<button type="button" ng-click="ok(0)">Submit</button>

Controller: 控制器:

$scope.ok = function () {
    if(!validationHasPassed()){
        //errorMsg.push('xxx')
        return false;
    }
    $scope.confirm();
};

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

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