javascript - has-error and has-success not working - Stack Overflow

I have created an Angular reactive form and using ngClass, I add Bootstrap's has-success and has-e

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?

Share Improve this question edited Oct 23, 2021 at 14:14 Vega 28.8k28 gold badges120 silver badges145 bronze badges asked Feb 12, 2018 at 5:25 Manu ChadhaManu Chadha 16.8k24 gold badges117 silver badges245 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 4

It 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745268803a4619619.html

相关推荐

  • javascript - has-error and has-success not working - Stack Overflow

    I have created an Angular reactive form and using ngClass, I add Bootstrap's has-success and has-e

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信