When submitting a form in a modal, how to perform multiple actions on submit?

So, I created a grid, and added a modal that displays on click. This modal includes a form for editing a contact. When I save the contact form in the modal, I’d like to hide the modal and reload the grid. But this does not appear to be working.

How should I do this?

/**** Adding New Contacts ****/

$modal = $app->add(['Modal', 'title' => 'Add Contact to ' . $c->get('company_name')]);
$modal->set(function ($p) use ($modal, $db, $c, $grid) {

    $contact = new Contact($db);

    $form2 = $p->add('Form');
    $form2->setModel($contact, false);

    $form2->addField('CompanyID',['Hidden']);
    $modal_layout = $form2->layout->addSubLayout();
    $modal_layout->add(['Header','Contact Details']);
    $modalDetailsGroup1 = $modal_layout->addGroup(['width'=>'three']);
    $modalDetailsGroup1->setModel($contact, ['FirstName','LastName','Salutation']);
    $modalDetailsGroup2 = $modal_layout->addGroup(['width'=>'three']);
    $modalDetailsGroup2->setModel($contact, ['PhoneNumber','PhoneExtention','AltNumber']);
    $modalDetailsGroup3 = $modal_layout->addGroup(['width'=>'three']);
    $modalDetailsGroup3->setModel($contact, ['MobilePhone','FaxNumber','JobTitle']);
    $modalDetailsGroup4 = $modal_layout->addGroup(['width'=>'two']);
    $modalDetailsGroup4->setModel($contact, ['Email']);
    $modalDetailsGroup5 = $modal_layout->addGroup(['width'=>'four']);
    $modalDetailsGroup5->setModel($contact, ['VotingRights','AlternateVoter','BoardMember']);
    $modalDetailsGroup6 = $modal_layout->addGroup(['width'=>'four']);
    $modalDetailsGroup6->setModel($contact, ['Eblast']);
    
    $form2->onSubmit(function($form) use ($modal, $grid, $contact){
        $form->model->save();
        $name = $contact->get('FirstName');
        
      /* NOT WORKING
         $modal->hide(); 
         new \atk4\ui\jsReload($grid);
     */

        return $form->success($name . 'has been added.');
    });
    $form2->add(['Button','Cancel'])->on('click', $modal->hide());

});
$item = $grid->menu->addItem('Add Contact')->on('click',$modal->show());

Here’s how I accomplish it using a virtual page containing a form, displayed as a modal. Can probably adapt it to your situation:

		$vp_Issues_form->onSubmit(function ($vp_Issues_form) use ($m_Issues, $issues_grid) {

			/* ... some other code that does some stuff ... */

			$m_Issues->save();
			$return = array();
			$return[] = (new \atk4\ui\jQuery('.atk-modal'))->modal('hide');
			$return[] = (new \atk4\ui\jsNotify('Record saved.'))->setColor('teal');
			/* Sort doesn't work.  Would need to obtain the column name from the caption (since it goes away when column is sorted in descending order) and then recreate the _sort parameter */
			$return[] = new \atk4\ui\jsReload($issues_grid->container, [$issues_grid->name.'_sort' => (new \atk4\ui\jQuery())->data('column')]);
			return $return;
		});

Thanks! This is perfect and helped answer a number of questions.