I’m a student in IT and i have a little problem in my intership.
I have to modify an objet in the but before that i have to bind the data of the object in the form but my form control is not found.
Here is my form :
this.ficheForm = this.formBuilder.nonNullable.group({
nomFiche: ['', Validators.required],
})
Here is how i create the form :
if(questionNode.type==='CHECKBOX'){
// Créer un FormArray pour les réponses de type CHECKBOX
const formArray = this.formBuilder.array([]);
// Boucler sur les réponses et ajouter un FormControl pour chaque réponse
questionNode.reponse.forEach(reponse => {
const formControl = this.formBuilder.control([]);
formArray.push(formControl);
});
// Ajouter le FormArray au FormGroup avec le nom de la question comme clé
this.ficheForm.addControl(questionNode.id, formArray);
}else{
this.ficheForm.addControl(questionNode.id, this.formBuilder.group({
objet: ['', Validators.required], // Premier champ requis
reponse: [''] // Deuxième champ optionnel
}));
}
And now how i try to bind data to the form :
if (this.ficheForm) {
// Parcours des entrées de la map questionReponse
for (let entry of Object.entries(this.reponses)) {
let [questionId, reponseInfo] = entry;
let [reponseId, responseValue] = Object.entries(reponseInfo)[0];
// Get the corresponding form control
const questionControl = this.ficheForm.controls[questionId];
if (questionControl) {
// Get the objet and reponse sub-controls
const objetControl = questionControl.get('objet');
const reponseControl = questionControl.get('reponse');
if (objetControl && reponseControl) {
// Set the values of the sub-controls
objetControl.setValue(reponseId);
reponseControl.setValue(responseValue);
// Log values for verification
console.log(objetControl.value);
console.log(reponseControl.value);
} else {
console.error(`Object or response control for question ID ${questionId} not found.`);
}
} else {
console.error(`Form control for question ID ${questionId} not found.`);
}
}
} else {
console.error('Form group not initialized.');
}
To bind the data in my form i recup the data from a Map<string, Map<string, string>>
And here is my result :
(https://i.sstatic.net/Cb3YQnCr.png)
Thanks for your time and your help.
Maxence Minucci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.