提交表单验证 - angularjs 1.5 - $ field $ pristine时无效

时间:2016-04-01 20:10:06

标签: javascript jquery angularjs forms validation

我正在以表格形式加载数据。我想只允许用户在表单有效时提交数据。最初形式是原始但无效。在用户可以编辑的三个字段中,如果他改变了任何一个,那么表单就不再是原始状态了,这是好的但是表单仍然无效......尽管其他字段中的值是有效的。我怎样才能绕过每个字段放置一个isPristine和IsValid条件?代码如下。我把它放在转发器里面进行编辑/查看。删除了代码的视图部分,因为我认为它在这种情况下不适用。

<table class="table">
            <tbody>
                <tr ng-repeat="comment in comments | orderBy: 'CreatedDate':true" ng-class-odd="'odd'" ng-class-even="'even'">
                    <td style="padding-left:1em;">

                        <ng-form name="editCommentForm">
                            <table border="0" class="form-group" ng-show="editComment.CommentID == comment.CommentID">
                                <tr>
                                    <td width="25%">Comment Type:</td>
                                    <td width="50%">
                                        <select name="cbCommentType" ng-model="comment.CommentTypeID" required>
                                            <option ng-selected="{{comment.CommentTypeID == option.CommentTypeID}}" ng-repeat="option in CommentTypes" value="{{option.CommentTypeID}}">{{option.Name}}</option>
                                        </select>
                                    </td>
                                    <td width="25%" class="errorMessage">
                                        <span ng-show="editCommentForm.cbCommentType.$error.required">Type is required.</span>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="3">&nbsp;</td>
                                </tr>
                                <tr>
                                    <td width="25%">Effective Date:</td>
                                    <td width="50%">
                                        <span class="input-group">
                                            <input type="text" name="txtEffectiveDate" class="form-control" uib-datepicker-popup ng-model="comment.EffectiveDate" is-open="editComment.popupOpened" ng-required="true" close-text="Close" />
                                            <span class="input-group-btn">
                                                <button type="button" class="btn btn-default" ng-click="openDatePicker(comment)"><i class="glyphicon glyphicon-calendar"></i></button>
                                            </span>
                                        </span>
                                    </td>
                                    <td width="25%" class="errorMessage">
                                        <p ng-show="editCommentForm.txtEffectiveDate.$error.required">Effective date is required.</p>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="3">&nbsp;</td>
                                </tr>
                                <tr>
                                    <td width="25%">Comment:</td>
                                    <td width="50%">
                                        <textarea name="txtComment" style="width:80%;resize:none;" name="txtComment" ng-model="comment.CommentText" placeholder="add comment here" ng-minlength="4" required> </textarea>
                                    </td>
                                    <td width="25%" class="errorMessage">
                                        <p ng-show="editCommentForm.txtComment.$error.required">Comment is required.</p>
                                        <p ng-show="editCommentForm.txtComment.$error.minlength">Comment is too short.</p>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="3" align="center">
                                        <input type="button" value="Cancel" ng-click="cancelObj(comment)" class="btn btn-default active btn-xs" />
                                        <input type="button" ng-disabled="editCommentForm.$pristine ? false : editCommentForm.$invalid" value="Save comment" ng-click="updateObj(comment)" class="btn btn-default active btn-xs" />
                                    </td>
                                </tr>
                            </table>
                        </ng-form>
                    </td>
                </tr>
            </tbody>
        </table>

1 个答案:

答案 0 :(得分:2)

你有1个表格,你有3个字段。

您可以让控制器访问该表单。

通常,为此,我使用CONTROLLER AS语法如下:

<ng-form name="vm.editCommentForm">

其中vm是控制器别名。

(我确信它也可以在没有CONTROLLER AS语法的情况下将表单暴露给控制器,但这是为了让你弄清楚是否对这种方法感兴趣。)

然后在你的控制器中你会做类似......

vm.showFieldError = function(fieldName) {
    var formField = vm.editCommentForm[fieldName];
    return (formField.$invalid && formField.$dirty);
}

然后您将在视图中引用此函数以确定是否显示错误消息。