gpt4 book ai didi

angularjs - 设置 $invalid 和 $error 不会使表单无效

转载 作者:搜寻专家 更新时间:2023-10-30 20:34:19 25 4
gpt4 key购买 nike

我有一个表单,我正在将我的电话字段从使用正则表达式更改为 intl-tel-input 模块。我在检查有效 #s 时遇到了问题和困惑。

我有一个包含其中几个字段的表单...

<div class="row">
<div class="col-xs-2">
<label for="cellPhone"
translate>CONTACT.CELL</label>
</div>
<div class="col-xs-6"
ng-class="{'has-error': !cellPhoneFocus && forms.contactForm.cellPhone.$invalid && forms.contactForm.cellPhone.$touched }">
<input type="text" class="form-control intlPhoneInput"
id="cellPhone" name="cellPhone"
ng-model="contact.cellPhone.display"
ng-focus="cellPhoneFocus = true"
ng-blur="cellPhoneFocus = false; validatePhone($event)">
<div ng-messages="forms.contactForm.cellPhone.$error"
ng-show="!cellPhoneFocus && forms.contactForm.cellPhone.$touched"
class="errorMessages">
<p ng-message="pattern" translate>
CRUD.VALID_PHONE</p>
</div>
</div>

然后提交...

<button type="submit" class="btn btn-primary" ng-disabled="forms.contactForm.$invalid" id="saveContactLink" translate>CRUD.SAVE</button>

然后在我的 Controller 中( typescript )...

//In Constructor
$scope.validatePhone = this.validatePhone.bind(this);

//Outside constructor
private validatePhone(eventObject: any) {
let thePhoneField = $('#' + eventObject.target.id);
let phoneIsValid = thePhoneField.intlTelInput("isValidNumber");

this.$scope.forms.contactForm[eventObject.target.id].$invalid = !phoneIsValid;
this.$scope.forms.contactForm[eventObject.target.id].$error = {"pattern": !phoneIsValid};
}

这正确地设置了我的 has-error 类和错误消息,但它没有将 FORM 设置为无效。我试过 $setValidity...

this.$scope.forms.contactForm[eventObject.target.id].$setValidity('pattern', !phoneIsValid);

...但它似乎根本没有做任何事情。

如何将字段设置为无效,显示正确的 ng-message(如果有多个),并确保更新 $errors 表单以便禁用提交?

最佳答案

原来 $setValidity 是工作的,但是 setValidity 与 .$invalid 和 $error 一起导致了一些交叉线。在使字段无效时注释掉最后两个可以正确启用/禁用我的提交按钮。

关于angularjs - 设置 $invalid 和 $error 不会使表单无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38976742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com