繁体   English   中英

当我使用angular-datepicker删除以前保存的日期时,该表单始终无效

[英]The form is always invalid when I erase previously saved date with angular-datepicker

这个问题很难解释,但是我会尝试的。

1)我填写表格并通过angular-datepicker在datetime字段中设置一个值。

2)按提交按钮。 表单通过验证,一切正常,表单已提交并保存在服务器上。

3)我想编辑以前完成的表格。 我再次打开表单,查看以前保存的结果。 如果我从datetime字段中删除了值,则无法再提交表单。 当我按下“提交”按钮时,什么也没有发生,也没有警报或错误。 我只能在调试的同时看到operationsClientForm.$validfalse

重要信息:

1)如果我只是更改datetime的值,但不会删除它,一切都会好的。 仅当我擦除该值时才会发生问题。

2)如果我第一次没有填写datetime字段,则将提交表单。 仅当我第一次填写日期时间并在编辑时将其删除时,才会发生问题。

3)绝对是客户端错误。

代码示例:

<md-input-container class="md-block defaultInput deskInput">
     <input type="text" date-time ng-model="client.date_of_birth" auto-close='true' timezone="UTC" format="DD.MM.YYYY" min-view="date" template='views\controls\datepicker.tpl.html' pattern="^(\d{2}).\d{2}.(\d{4})$">
</md-input-container>

$scope.saveClick = function (operationsClientForm) {
        if (operationsClientForm.$valid) {
              //submitting
        }
}

<md-button class="md-raised rightAlign operationButton" ng-click="saveClick(operationsClientForm)" type="submit">
    <translate>Save</translate>
</md-button>

因此,我发现了问题的可能原因。 当我们从datetime字段中删除一个值时,模型内部的值将变为undefined而不是"" 这会导致角度形式验证器内部出现解析错误。 一个明显的想法(同时也是愚蠢的)是在模型中用""代替undefined ,然后再次强制角度验证器。

它是这样的:

<md-input-container class="md-block defaultInput deskInput">
    <input ng-blur="onDateTimeBlur($event)" type="text" date-time ng-model="client.date_of_birth" auto-close='true' timezone="UTC" format="DD.MM.YYYY" min-view="date" template='views\controls\datepicker.tpl.html' pattern="^(\d{2}).\d{2}.(\d{4})$">
</md-input-container>

    $scope.onDateTimeBlur = function (event) {
        if (event.currentTarget) {
            var modelReference = event.currentTarget.attributes["ng-model"].value;
            if (event.currentTarget.value == "") {
                this.value = "";
                this.setDeepValue(modelReference, "");
            }
        }
    };

    $scope.reValidateParseErrors = function (form) {
        var initialErrorsCount = 0;
        if (form.$error.parse) {
            initialErrorsCount = form.$error.parse.length; //to prevent infinite loop
            while (form.$error.parse) {
                form.$error.parse[0].$validate();
                if (form.$error.parse && form.$error.parse.length == initialErrorsCount) {
                    break; //break the loop if real parse errors exist
                }
            }
        }
    };

$scope.saveClick = function (operationsClientForm) {
    this.reValidateParseErrors(operationsClientForm);
    if (operationsClientForm.$valid) {
          //submitting
    }
}

无论如何,这个问题仍然存在,因为我的解决方案似乎使我不堪重负。

暂无
暂无

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

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