How to filter values from a dropdown field by taking into account value from another dropdown previously set #658
-
Greetings. I hope someone can help me with the following. I have a form to put prices to cars. Inside this form, I have a series of fields, from which, two are relation-typed dropdowns: models and versions. Each model can have many versions. As some versions might have the same name, I need to figure out a way to implement some functionality oriented to filter the Versions field by taking into account the value set previously for the Models field. Otherwise, I might assign the incorrect price to the incorrect version. I read the documentation and perhaps what I need has to do with either Events or Field Dependency. But I have to be honest, I have no clue on how to proceed. Could someone kindly enlighten the way for me?. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You're looking for Field Dependency and model option methods. Example: fields.yaml model:
label: Model
type: dropdown
version:
label: Version
type: dropdown
dependsOn: model MyModel.php public function getModelOptions()
{
return ModelModel::lists('name', 'id');
}
public function getVersionOptions()
{
if ($this->model) {
return $this->model->versions()->lists('name', 'id');
}
return Version::lists('name', 'id');
} |
Beta Was this translation helpful? Give feedback.
You're looking for Field Dependency and model option methods.
Example:
fields.yaml
MyModel.php