mdb-date-picker doesn't work with formControlName


Topic: mdb-date-picker doesn't work with formControlName

mdb2 pro asked 6 years ago

How can I use mdb-date-picker with formControlName under a parent formGroup directive and not as ngModel ?

Damian Gemza staff commented 6 years ago

Dear mdb2, Could you please provide me a little bit more information about your question? It would help me to answer your question. Best Regards, Damian

mdb2 pro commented 6 years ago

This code throws a runtime error סוג מסמך

mdb2 pro answered 6 years ago

<form [formGroup]="form" (ngSubmit)="submit()"> <div class="col-md-12"> <div class="card contact-card with-padding"> <div class="row"> <div class="col-md-1"> <div class="md-form" style="direction:ltr"> <!-- mdb-date-picker cannot use formControlName https://stackoverflow.com/questions/45659742/angular4-no-value-accessor-for-form-control --> <mdb-date-picker [options]="myDatePickerOptions" formControlName="sDate" [placeholder]="'Selected date'" [(ngModel)]="model" required></mdb-date-picker> </div> </div> </div> </div> </div> </form>

Arkadiusz Idzikowski staff answered 6 years ago

Dear mdb2, You need to remove ngModel from mdb-date-picker, then add ReactiveFormsModule to your app module and declare new FormGroup in ts file.
import { Component, OnInit } from '@angular/core';

import { FormControl, FormGroup } from '@angular/forms';

@Component({

selector:'app-root',

templateUrl:'./app.component.html',

styleUrls: ['./app.component.scss']

})

export class AppComponent implements OnInit {

form:FormGroup;

submit() {

console.log(this.form);

}

ngOnInit() {

this.form=newFormGroup({

sDate:newFormControl()

});

}

}
  Best, Arek

mdb2 pro commented 6 years ago

I removed [(ngModel)]=”model” and placed the picker inside a working reactive form Error is: ERROR Error: No value accessor for form control with path: 'type -> sDate' Code is: mdb-date-picker [options]="myDatePickerOptions" formControlName="sDate" [placeholder]="'Selected date'"></mdb-date-picker

Arkadiusz Idzikowski staff commented 6 years ago

Dear mdb2, Did you add 'sDate' as newFormControl of FormGroup in your ts file (the last lines of code from my example) and ReactiveFormsModule? Please provide an example of your typescript code so I can try to recreate this problem. Best, Arek

mdb2 pro commented 6 years ago

Yes, type.sDate exists in the form and the form is reactive. It is a part of a big form so it is difficult for me to share Since 'type' is the name of a form group, I tried to elevate the control to be directly under form but same results. I also tried to replace the date picker with an input and it works: input type="text" readonly formControlName="sDate" class="form-control" [mdbValidate]="false" mdbInputDirective Can you please point me to your documentation where there is an example of mdb-date-picker using formControl ?

Arkadiusz Idzikowski staff commented 6 years ago

Dear mdb2, Did you try to use my example ts code with the html code you added to the previous post? Does the console show any errors in this case? Using this example I managed to get the date from date picker in my component ts file. Is this what you tried to achieve? Best, Arek

mdb2 pro commented 6 years ago

I sent Damian a sample app with DatePicker using formControl error

Arkadiusz Idzikowski staff commented 6 years ago

Dear mdb2, I just checked example you sent to us and you are missing 'Pro' in your MDBBootstrapModule import. It should be MDBBootstrapModulePro.forRoot(). Best, Arek

mdb2 pro answered 6 years ago

Thanks. Now it works. Some how I missed it
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
  • User: Pro
  • Premium support: No
  • Technology: MDB Angular
  • MDB Version: -
  • Device: -
  • Browser: -
  • OS: -
  • Provided sample code: No
  • Provided link: No