I'm submitting a ... (check one with "x")
[ ] bug report => search GitHub for a similar issue or PR before submitting
[ ] feature request
[ X ] support request
I'm trying to create a complex form, using field group and repeaters, flat and nested. Therefore, the formControl could be complicated a lot since there could be nested controls. But that's ok.
My problem is that I want to run some logic on the formControl of a specific field. E.g., adding a Subscription to the statusChange or valueChange. So I thought, since I'm using ngx-formly 5, I could try the hooks and using the formControl when the form has been created. So my field configuration might seem something like this:
fields: FormlyFieldConfig[] = [
{
key: 'repeater',
type: 'repeat',
templateOptions: {
addText: 'Add',
},
fieldArray: {
fieldGroup: [
{
className: 'col-sm-4',
type: 'input',
key: 'repeatedInput',
hooks: {
onInit:(field: FormlyFieldConfig) => {
// Here I need the formControl of the specific field, since something will happen here
}
},
templateOptions: {
label: 'Input:',
required: true,
},
}
],
},
},
];
Current behaviour
the onInit has as argument a field: FormlyFieldConfig property with some information about the field and a sub-property called formControl which include the control of the entire structured form.
Desired behaviour
I'd like to be able to have a clean property containing the formControl of the selected field.
Since the formControl has a nested structure, I can navigate the tree. But If I have a repeater (like the structure posted above), I get a formArray, and I don't know how to get the formControl of (let's say) 4th repeatedInput inside the repeater field.
I might be missing something, so thank you for any help.
TL;DR: Is there a way to get the formControl as it is inside the hooks property of a specific field? If not, do you think could it be possible to have this feature?
Thanks in advance for any help.
I don't know how to get the formControl of (let's say) 4th repeatedInput inside the repeater field.
each field instance has a formControl and form instance which the parent form control, there is several ways to get the 4th repeatedInput, here is one of them:
hooks: {
onInit:(field: FormlyFieldConfig) => {
if (field.parent.key === '3') {
console.warn(field.formControl);
}
}
},
@aitboudad thank you for your response, but this is not helping me at all, unfortunately, since I might need to get the n-th repeatedInput inside the repeatedField dinamically. Is there a way, to access to the FormArray from the child FormGroup knowing which index in the array has the child FormGroup assigned?
in the above example, you can get the FormArray and the FormGroup from the parent
field.parent.formControl // will return FormGroup
field.parent.form // will return FormArray
to access to the FormArray from the child FormGroup knowing which index in the array has the child FormGroup assigned?
field.parent.form.at(field.parent.key) // which will return the same result as `field.parent.formControl`
Most helpful comment
in the above example, you can get the
FormArrayand theFormGroupfrom the parent