Pass values from the parent form to the child form
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);
}
There are no comments yet
Be the first one to comment