Search code examples
angulardatepickerngx-bootstrap

Error with datepicker ngx-bootstrap and Validators Angular 4


When I started to use the datepicker, the html changed to:

<div class="form-group col-md-4">
<label for="arrivalDate">Entry and Departure</label>
<input formControlName="arrivalDate" class="form-control" id="arrivalDate" placeholder="mm/dd/yyyy - mm/dd/yyyy" bsDaterangepicker [minDate]="minDate" [bsConfig]="dpConfig">

That have a component the next:

this._form = this.fb.group({
   // Search basic
   destination: ['', Validators.required],
   arrivalDate: [null, Validators.required],
   qtyAdults: [0, [Validators.required, Validators.min(1) ]],
   qtyChilds: [0, Validators.required ],
   childsage: this.fb.array([]),
   adultsage: this.fb.array([]),
   serviceType: [],
   qtyProduct: ['1', Validators.min(1)]
});

So, while it was the input type date the Validators did not fail, but when changing it to the datepicker of ngx-bootstrap, it gives error.

The error in the next:

Error: There is no FormControl instance attached to form control element with name: 'arrivalDate'

Before when I was using does not gives error, but with this datepicker, started to give that error

I'm using this https://valor-software.com/ngx-bootstrap/#/datepicker


Solution

  • In the html I added a div with:

    *ngIf = "isFormHotelActivate"
    

    and in the component:

    _initFormHotels() {
        this._formT = this.fb.group({
           // Search basic
           origin: [''],
           destination: ['', Validators.required],
           arrivalDate: [null, Validators.required],
           qtyProduct: [1, Validators.min(1)]
        });
    this.isFormHotelActivate = true;
    }