src/models/traits/AssignSwitchTrait.php
protected function generateUniqueValidators(): array
    {
        if (empty($this->switchVariants)) {
            throw new InvalidConfigException('Please specify `switchVariants` array to use AssignSwitchTrait::generateUniqueValidators()');
        }
        $rules = [];

        foreach ($this->switchVariants as $variant) {
            $rules[] = [
                [$variant . '_port'],
                function ($attribute, $params, $validator) use ($variant) {
                    if ($this->{$attribute} && $this->{$variant . '_id'}) {
                        $query = Binding::find();
                        $query->andWhere(['port' => $this->{$attribute}]);
                        $query->andWhere(['switch_id' => $this->{$variant . '_id'}]);
                        $query->andWhere(['ne', 'base_device_id', $this->id]);
                        /** @var Binding[] $bindings */
                        $bindings = $query->all();
                        if (!empty($bindings)) {
                            $binding = reset($bindings);
                            $this->addError($attribute, Yii::t('hipanel:server', '{switch}::{port} already taken by {device}', [
                                'switch' => $binding->switch_name,
                                'port' => $binding->port,
                                'device' => $binding->device_name,
                            ]));
                        }
                    }
                },
            ];
        }

        return $rules;
    }
protected function generateUniqueValidators(): array
    {
        if (empty($this->switchVariants)) {
            throw new InvalidConfigException('Please specify `switchVariants` array to use AssignSwitchTrait::generateUniqueValidators()');
        }
        $rules = [];

        foreach ($this->switchVariants as $variant) {
            $rules[] = [
                [$variant . '_port'],
                function ($attribute, $params, $validator) use ($variant) {
                    if ($this->{$attribute} && $this->{$variant . '_id'}) {
                        $query = Binding::find();
                        $query->andWhere(['port' => $this->{$attribute}]);
                        $query->andWhere(['switch_id' => $this->{$variant . '_id'}]);
                        $query->andWhere(['ne', 'base_device_id', $this->id]);
                        /** @var Binding[] $bindings */
                        $bindings = $query->all();
                        if (!empty($bindings)) {
                            $binding = reset($bindings);
                            $this->addError($attribute, Yii::t('hipanel:server', '{switch}::{port} already taken by {device}', [
                                'switch' => $binding->switch_name,
                                'port' => $binding->port,
                                'device' => $binding->device_name,
                            ]));
                        }
                    }
                },
            ];
        }

        return $rules;
    }
protected function generateUniqueValidators ( ) : array { if ( empty ( $ this -> switchVariants ) ) { throw new InvalidConfigException ( 'Please specify `switchVariants` array to use AssignSwitchTrait::generateUniqueValidators()' ) ; } $ rules = [ ] ; foreach ( $ this -> switchVariants as $ variant ) { $ rules [ ] = [ [ $ variant . '_port' ] , function ( $ attribute , $ params , $ validator ) use ( $ variant ) { if ( $ this -> { $ attribute } && $ this -> { $ variant . '_id' } ) { $ query = Binding :: find ( ) ; $ query -> andWhere ( [ 'port' => $ this -> { $ attribute } ] ) ; $ query -> andWhere ( [ 'switch_id' => $ this -> { $ variant . '_id' } ] ) ; $ query -> andWhere ( [ 'ne' , 'base_device_id' , $ this -> id ] ) ; /** @var Binding[] $bindings */ $ bindings = $ query -> all ( ) ; if ( ! empty ( $ bindings ) ) { $ binding = reset ( $ bindings ) ; $ this -> addError ( $ attribute , Yii :: t ( 'hipanel:server' , '{switch}::{port} already taken by {device}' , [ 'switch' => $ binding -> switch_name , 'port' => $ binding -> port , 'device' => $ binding -> device_name , ] ) ) ; } } } , ] ; } return $ rules ; }
Added to model's rules list of switch pairs.

@throws InvalidConfigException
@return array
Added to model s rules list of switch pairs .