'Champs obligatoire'], ['id_producer', 'integer'], ['id_producer', function($attribute, $params) { if ($this->id_producer) { $producer = Producer::findOne($this->id_producer); if (!$producer) { $this->addError($attribute, 'Ce producteur n\'existe pas.'); } } }], ['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { $producer = Producer::findOne($this->id_producer); if ($producer) { return strlen($producer->code); } else { return false; } }], ['code', function($attribute, $params) { $code = $this->$attribute; $producer = Producer::findOne($this->id_producer); if ($producer && strtolower(trim($code)) != strtolower(trim($producer->code))) { $this->addError($attribute, 'Code incorrect'); } }], ]; } public function attributeLabels() { return [ 'id_producer' => 'Producteur', 'code' => 'Code du producteur', ]; } }