admin管理员组文章数量:1417713
I have created an Angular reactive form and using ngClass
, I add Bootstrap's has-success
and has-error
classes depending on whether a form field is valid or not
/*checks if data inthe field is valid or not*/
isFieldValid(field: string) {
let fieldValid:boolean = this.signupForm.get(field).valid && this.signupForm.get(field).touched;
console.log("isFieldValid for "+field+" returning "+fieldValid);
return fieldValid;
}
/*add css to this control depending on whether it is valid or not*/
displayFieldCss(field: string) {
console.log("in displayFieldCss for field "+field);
let fieldValid:boolean = this.isFieldValid(field);
return {
'has-error': !fieldValid, // if fieldvalid returns false i.e. field is not valid then we want has-error css. So set it to true (opp. of fieldValid)
'has-success': fieldValid //if fieldvalid returns true i.e. field is valid then we want has-success css. So set it to true (value of fieldValid)
//'has-feedback': this.isFieldValid(field)
}
HTML
<label for="firstname" class="control-label required">First Name</label>
<input id="firstname" type="text" class="form-control" formControlName="firstName" [ngClass]="displayFieldCss('firstName')" required>
<app-show-errors [control]="signupForm.controls.firstName"></app-show-errors>
On inspecting the element in the browser, I can see that the classes are being added.
Inspect element if the field is empty
<input _ngcontent-c7="" class="form-control ng-pristine ng-invalid ng-touched has-error" formcontrolname="firstName" id="firstname" required="" ng-reflect-klass="form-control" ng-reflect-ng-class="[object Object]" ng-reflect-required="" ng-reflect-name="firstName" type="text">
Inspect element if the field is empty
<input _ngcontent-c7="" class="form-control ng-touched ng-dirty ng-valid has-success" formcontrolname="firstName" id="firstname" required="" ng-reflect-klass="form-control" ng-reflect-ng-class="[object Object]" ng-reflect-required="" ng-reflect-name="firstName" type="text">
But I don't see that input
text box being bordered in red color for has-error
or green color for has-success
. Isn't this the default behavior of Bootstrap or do I need to provide css
for this?
I have created an Angular reactive form and using ngClass
, I add Bootstrap's has-success
and has-error
classes depending on whether a form field is valid or not
/*checks if data inthe field is valid or not*/
isFieldValid(field: string) {
let fieldValid:boolean = this.signupForm.get(field).valid && this.signupForm.get(field).touched;
console.log("isFieldValid for "+field+" returning "+fieldValid);
return fieldValid;
}
/*add css to this control depending on whether it is valid or not*/
displayFieldCss(field: string) {
console.log("in displayFieldCss for field "+field);
let fieldValid:boolean = this.isFieldValid(field);
return {
'has-error': !fieldValid, // if fieldvalid returns false i.e. field is not valid then we want has-error css. So set it to true (opp. of fieldValid)
'has-success': fieldValid //if fieldvalid returns true i.e. field is valid then we want has-success css. So set it to true (value of fieldValid)
//'has-feedback': this.isFieldValid(field)
}
HTML
<label for="firstname" class="control-label required">First Name</label>
<input id="firstname" type="text" class="form-control" formControlName="firstName" [ngClass]="displayFieldCss('firstName')" required>
<app-show-errors [control]="signupForm.controls.firstName"></app-show-errors>
On inspecting the element in the browser, I can see that the classes are being added.
Inspect element if the field is empty
<input _ngcontent-c7="" class="form-control ng-pristine ng-invalid ng-touched has-error" formcontrolname="firstName" id="firstname" required="" ng-reflect-klass="form-control" ng-reflect-ng-class="[object Object]" ng-reflect-required="" ng-reflect-name="firstName" type="text">
Inspect element if the field is empty
<input _ngcontent-c7="" class="form-control ng-touched ng-dirty ng-valid has-success" formcontrolname="firstName" id="firstname" required="" ng-reflect-klass="form-control" ng-reflect-ng-class="[object Object]" ng-reflect-required="" ng-reflect-name="firstName" type="text">
But I don't see that input
text box being bordered in red color for has-error
or green color for has-success
. Isn't this the default behavior of Bootstrap or do I need to provide css
for this?
1 Answer
Reset to default 4It seems that in bootstrap4, has-error and has-succes were dropped. Use "is-valid" and "is-invalid" instead:
...
return {
'is-invalid': !fieldValid, // if fieldvalid returns false i.e. field is not valid then we want has-error css. So set it to true (opp. of fieldValid)
'is-valid': fieldValid //if fieldvalid returns true i.e. field is valid then we want has-success css. So set it to true (value of fieldValid)
//'has-feedback': this.isFieldValid(field)
}
...
DEMO
本文标签: javascripthaserror and hassuccess not workingStack Overflow
版权声明:本文标题:javascript - has-error and has-success not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745268802a2650764.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论