The validation works but I always get the same error message linked with required when the input error is about min/max.
And another issue that the error message is only shown when i click outside the input first time.
component.ts
import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { FormControl, Validators, FormBuilder, FormGroup} from '@angular/forms';
export class myClass implements OnInit {
myform: FormGroup;
constructor (private _fb:FormBuilder){}
public someAmount = new FormControl('', [Validators.required, Validators.min(10), Validators.max(300)]);
ngOnInit():void {
this.myform = this._fb.group({
someAmount: ['', [Validators.required, Validators.min(10), Validators.max(300)]]
});
}
}
component.html
<form novalidate [formGroup]="myform" class="col-sm-4 form-group">
<md-input-container>
<input mdInput id="amount"
name="someAmount"
formControlName="someAmount"
type="number"
class="form-control "
[(ngModel)]="amount"
placeholder="amount"
(keyup)="onAmountChange()"/>
<md-error *ngIf="someAmount.hasError('required')">
<span> number required </span>
</md-error>
<md-error *ngIf="someAmount.hasError('min')" class="top-margin-medium text-center bold alert-danger">
<span>amount should be not less than 10 </span>
</md-error>
<md-error *ngIf="someAmount.hasError('max')" class="top-margin-medium text-center bold alert-danger">
<span>amount should be not more than 300</span>
</md-error>
</md-input-container>
</form>