Topic: Use variable for form validation error message

christso pro asked 5 years ago


I'm trying to use a variable to set the error message that appears when the input is invalid. Here's my html code:
<div class="md-form mt-0 input-group">
  <input formControlName="amount" class="form-control" type="number" class="form-control" id="amount" mdbInputDirective [data-error]="getErrorMessage()">
</div>
However, the message shows as the default message "wrong". It seems that variables are not supported. How do I get the error message from Angular instead of hardcoding it in the html?

Arkadiusz Idzikowski staff answered 5 years ago


Dear christso, If you need to assing variable to error/success message you can use new inputs [errorMessage] and [successMessage]. Regards, Arek

christso pro answered 5 years ago


I removed the mdbInputDirective, and added this to my html file:
<span class="inputVal text-danger" style="visibility: visible; top: 37px;" *ngIf="editForm.controls.amount.invalid">{{getErrorMessage()}}.</span>
That seems to work. I was expecting a more elegant solution with MDBootstrap.


Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Resolved

Specification of the issue

  • ForumUser: Pro
  • Premium support: No
  • Technology: MDB Angular
  • MDB Version: 6.2.3
  • Device: Laptop
  • Browser: Chrome
  • OS: Windows 10
  • Provided sample code: No
  • Provided link: No