'参数错误'], ['filePath', 'validateFilePath'], ]; } public function attributeLabels() { return [ 'filePath' => '图片路径', ]; } public function validateFilePath($attribute) { $filePath = ltrim(strval($this->$attribute), '/'); $this->$attribute = $filePath; if ($filePath === '') { $this->addError($attribute, '参数错误'); return; } $controller = Yii::$app->controller; $mainId = intval($controller->mainId ?? 0); $shopId = intval($controller->shopId ?? 0); $prefix = HomePageConfigClass::BANNER_OSS_ROOT . '/' . $mainId . '/' . $shopId . '/'; if (strpos($filePath, $prefix) !== 0) { $this->addError($attribute, '无权删除该图片'); } } /** * @return string */ public function getFilePath() { return ltrim(strval($this->filePath), '/'); } }