This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Cpt.Meatball
Cpt.Meatball

Hi!

I'm trying to filter my dropdowns through the 'dependsOn' feature in my fields.yaml. After 3 hours of trying I figured out through some forum posts and looking at Rainlab's User plugin -> Country /state filtering, that the way the docs explain filtering is no longer functioning.

At this point my dropdowns do update when the dependency is updated, but I can't get the value of the dependency in my controller. The docs say: $this->*fieldname* but that doesn't work.

Code example below:

fields:
    poule:
        label: rebel59.matchmaking::lang.matches.poule
        type: dropdown
    home_team_id:
        label: rebel59.matchmaking::lang.matches.home_team
        type: dropdown
        dependsOn: poule

Model:

public function getPouleOptions()
{
    return Poule::getNameList();
}

public function getHomeTeamIdOptions()
{
    return Team::getNameList($this->poule);
}

The value of $this->poule is zilch.

Anybody know what I'm doing wrong?

Update as requested:

Poule.php (Model)

public static function getNameList()
{
    return self::all()->lists('name', 'id');
}

Team.php (Model)

public static function getNameList($pouleId)
{
    return self::where('poule_id',$pouleId)->lists('name', 'id');
}

Last updated

raw99
raw99

Please post these 2 static methods from "Poule" and "Team" models: Poule::getNameList(); and Team::getNameList($this->poule);

Cpt.Meatball
Cpt.Meatball

raw99 said:

Please post these 2 static methods from "Poule" and "Team" models: Poule::getNameList(); and Team::getNameList($this->poule);

I've updated the first post! I can confirm both static methods work. I can replace $pouleId by a valid integer and the dropdown is filled with data from my database.

Last updated

raw99
raw99

Ok, here is what should be in Poule model:

    protected static $nameList = null;
    public static function getNameList()
    {
        if (self::$nameList)
            return self::$nameList;

        return self::$nameList = self::lists('name', 'id');
    }

Team model:

    protected static $nameList = [];

    public static function getNameList($typeId)
    {
        if (isset(self::$nameList[$typeId]))
            return self::$nameList[$typeId];

        return self::$nameList[$typeId] = self::wherePouleId($typeId)->lists('name', 'id');
    }

Last updated

raw99
raw99

But you will need one more trick to inject value when form is opened for first time... Will check my code...

Cpt.Meatball
Cpt.Meatball

I can confirm that doesn't work. :P

Edit: Sorry didn't see your third comment!

Last updated

raw99
raw99
    public function filterFields($fields, $context = null)
    {
        if ($this->poule < 1) {
            $this->poule = 1;
    }

All should be working now... Check if I placed correct values - I made copy->paste from other project... ;)

Last updated

Cpt.Meatball
Cpt.Meatball

$this->poule never has a value. It doesn't get filled from the formfield..

Cpt.Meatball
Cpt.Meatball

Do I need an import to get the current value of the form-fields?

raw99
raw99

Cpt.Meatball said:

$this->poule never has a value. It doesn't get filled from the formfield..

ok, let's check it:

public function getHomeTeamIdOptions()
{
    var_dump($this->poule);
    return Team::getNameList($this->poule);
}

This will simply echo what value $this->poule has....

Last updated

Cpt.Meatball
raw99
raw99

Cpt.Meatball said:

returns NULL .

Try to change first dropdown value - does it return "NULL" all the time? Have you set up model relations?

Cpt.Meatball
Cpt.Meatball

It's not a RelationRender .. well it SHOULD be, but then I tried to do it like this. Anyway:

Poule Model

public $hasMany = [
    'teams' => ['Rebel59\Matchmaking\Models\Team']
];

Team Model

public $belongsTo = [
    'poule' => ['Rebel59\Matchmaking\Models\Poule'],
    'club' => ['Rebel59\Matchmaking\Models\Club']
];

Last updated

raw99
raw99

Cpt.Meatball said:

It's not a RelationRender .. well it SHOULD be, but then I tried to do it like this. Anyway:

config_relation.yaml of Match Model

poule: label: rebel59.matchmaking::lang.matchmaking.poule view: form: $/rebel59/matchmaking/models/poule/fields.yaml toolbarButtons: link manage: form: $/rebel59/matchmaking/models/poule/fields.yaml list: $/rebel59/matchmaking/models/poule/columns.yaml

I am not talking about Relation Manager but about model relations - does "Poule" and "Team" knows about each other?

Cpt.Meatball
Cpt.Meatball

I've updated above ;)

raw99
raw99

Cpt.Meatball said: relations of Match Model

public $belongsTo = [
   'home' => ['Rebel59\Matchmaking\Models\Team', 'home_team'],
   'away' => ['Rebel59\Matchmaking\Models\Team', 'away_team'],
   'poule' => ['Rebel59\Matchmaking\Models\Poule']

And here comes your mistake ^ $this->poule = Rebel59\Matchmaking\Models\Poule Change name from "Poule" to something different in all methods and it will work then....

Last updated

Cpt.Meatball
Cpt.Meatball

So if I change the field name it should work? Going to try it right away.

raw99
raw99

Cpt.Meatball said:

So if I change the field name it should work? Going to try it right away.

Change field name and it will work.... I simply added "poule_id" and etc for my form fields but you can choose anything... Don't forget to update all method names and variables too...

Last updated

Cpt.Meatball
Cpt.Meatball

Hmm, changing the name of my relation did help. But the weird thing is when I do: var_dump($this->poule_id) it shows String(1): "1" which is caused by the filter filterFields method. Strange thing is that references $this->poule and not $this->poule_id.

Oh thanks a lot for this awesome act of kindness hehe! I'm a lot further as I was an hour ago :D

raw99
raw99

Cpt.Meatball said:

Hmm, changing the name of my relation did help. But the weird thing is when I do: var_dump($this->poule_id) it shows String(1): "1" which is caused by the filter filterFields method. Strange thing is that references $this->poule and not $this->poule_id.

Oh thanks a lot for this awesome act of kindness hehe! I'm a lot further as I was an hour ago :D

filterFields method is used only for cheating these lists when form is opened for first time - I don't like to see empty dropdown lists, they are confusing me... ;) XD

1-20 of 33

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.