Nested relation forms and refreshing fields in v2

I have a project that’s running on version 2 and i’m struggling to refresh a field in the relation widget manage form, after saving a form in a third popup. I’ll try and explain.

I have 3 models.

hasMany: Contacts, Deals

belongsTo: Company
hasMany: Deals

belongsTo: Company, Contact

On the company preview page, i’m using relationRender(‘deals’) which shows me a create deal button and a list of the company’s deals.

When clicking the Create Deal button, the deal form defined in Companies controller config_relation is showing in a modal and this is all working great, as expected.

One of the fields on the deal form is a partial, where I have a button to Create a contact. When pressed, a second modal is shown displaying the create contact form. I have this form working and I’m able to create a contact and have it be assigned to the original company that the user is previewing.

The bit i’m struggling with is populating the deal’s contact field in the manage relation form with the newly created contact.

From the Companies controller, I’m not sure how I can render the company’s deal’s contact form field.

Hope all that makes sense, appreciate any help on this.

The changeHandler property may be useful here:

   label: Type
   type: dropdown
   placeholder: Select type
   changeHandler: onChangeContentBlockType

You can use it to refresh fields

public function onChangeContentBlockType()
    Flash::success('Great job!');

    return $this->formRefreshFields(['_internal_comments', 'is_positive']);

But since it is buried in a relation manager you may need the relationExtendManageWidget controller override.

1 Like

Took me about 4 hours to figure this out :joy:

I ended up initialising the Deals relation on the original Company model, grabbing the formWidget for that relation and returning the re-rendered contact field on the deal form.

Works like a charm :slight_smile:

$this->initRelation($company, 'deals');

$dealForm = $this->widget->relationDealsManageForm;
$dealForm->setFormValues(['contact' => $contact->id]);

return [
    '#Form-relationDealsManageForm-field-Deal-contact' => $dealForm->renderField('contact')

I will fully get to grips with how the relation controller behavior works eventually.

1 Like