October CMS resources and help articles

Simple and to the point. Optimized by the community.

Pass values from the parent form to the child form

0
by yfktn, last modified on November 28th, 2024

There is a need to pass values from the parent form to the child form using relationship behavior in the Backend of an application.

In the parent controller, the relationExtendManageWidget() function is overridden, and the model used in the widget of the child form is accessed using $widget->config->model. Then, a dynamic property is added to the model using the addDynamicProperty() function, so that this property is not saved to the database, as it is only used for filtering logic.

public function relationExtendManageWidget($widget, $field, $model) 
{
    if($field == 'actresses' 
        && property_exists($widget->config, 'context')
    )
    {
        $widget->config->model->addDynamicProperty('_active_continent_id', $model->continent_id);
    }
}

To use it, for example, there is a dropdown in the child form to display a list of countries in the selected continent previously selected in the parent form, then we simply do:

country:
    label: 'Country'
    nameFrom: name
    span: full
    type: relation
    scope: '\Foo\Bar\Actress::getFilteredCountryBasedOnContinent'

Then, the function is filled in as follows:

static public function getFilteredCountryBasedOnContinent($query, $model)
{
    $query
        ->where('continent_id', $model->_active_continent_id);
}

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?