My loginform is has 2 childs components (one for email and one for password).
I use formGroup to store input entries. On loading I have an error
- "No value accessor for form control name: ’email’"
- "No value accessor for form control name: ‘password’.
and some informations are missing on display.
After clicking submit button, missing informations appear on the screen and submit is working
My code:
html parent
<form [formGroup]="loginFormGroup" (submit)="validateLoginData()">
<div class="login">
<!--button container-->
<div class="login__input__container">
<!-- login-->
<app-input
[formGroup]="loginFormGroup"
type="text"
formControlName="email"
placeHolder="[email protected]"
name="email"
id="email"
labelText="email"
></app-input>
<!-- password-->
<app-input
[formGroup]="loginFormGroup"
type="password"
formControlName="password"
placeHolder="mot de passe"
name="password"
id="password"
labelText="password"
></app-input>
</div>
<div class="login__button__container">
<input type="submit" value="se connecter">
</div>
</div>
</form>
ts parent
ngOnInit() {
this.initializeLoginFormGroup();
}
/**
* Construction du formgroup
*/
initializeLoginFormGroup() {
this.loginFormGroup = this.fb.group({
// Email
email: ['', Validators.required],
// Password
password: ['', Validators.required]
})
}
html child
<div class="input__group" [formGroup]="formGroup">
<label class="input__label" [for]="id">{{labelText}}</label>
<input
class="input"
[type]="type"
[formControlName]="formControlName"
[placeholder]="placeHolder"
[name]="name"
[id]="id"
>
<!-- Erreur Message-->
<div>
<p>{{errorMessage}}</p>
</div>
</div>
ts child
export class InputComponent {
@Input() id: string = '';
@Input() type: string = '';
@Input() formControlName: string = '';
@Input() placeHolder: string = '';
@Input() name: string = '';
@Input() errorMessage: string ='';
@Input() labelText: string = ''
@Input() formGroup: FormGroup = new FormGroup({});
}
Hope you can help to solve this error
2
Answers
Indeed I've to implement controlValueAccessor to make work my formControl.
Here is my updated code with error event:
thanks for your help
Cyrille
Don’t need a formBuilder but rather a plain FormGroup. I would modify the TS file this way and it should work perfectly. (In the parent)
And, for the child, delete this from @Input:
change it to this:
child html:
child css: