Browse Source

Update AllowTagString.php

KIM-JINO5 5 months ago
parent
commit
cfc9ba3535
1 changed files with 21 additions and 5 deletions
  1. 21 5
      app/Rules/AllowTagString.php

+ 21 - 5
app/Rules/AllowTagString.php

@@ -6,6 +6,8 @@ use Illuminate\Contracts\Validation\Rule;
 
 class AllowTagString implements Rule
 {
+    protected string $errorType = '';
+
     /**
      * Create a new rule instance.
      *
@@ -25,14 +27,24 @@ class AllowTagString implements Rule
      */
     public function passes($attribute, $value)
     {
-        $value = aTrim($value); // 공백 제거
-        $isAlphaCommaStr = ((mb_detect_encoding($value, 'UTF-8') === true) ? (bool)preg_match('/^[-\pL\pN_, .]++$/uD', (string)$value) : (bool)preg_match('/^[-a-z0-9_, .]++$/iD', (string)$value));
-        if ($value && !$isAlphaCommaStr) {
+        $value = aTrim($value);
+
+        if ($value === '') {
+            return true;
+        }
+
+        // 콤마 개수 (태그 10개 = 콤마 9개)
+        if (substr_count($value, ',') > 9) {
+            $this->errorType = 'max';
             return false;
         }
-        if (substr_count($value, ',') > 10) {
+
+        // 각 태그는 숫자/영문/한글만 허용
+        if (!preg_match('/^[0-9A-Za-z가-힣]+(,[0-9A-Za-z가-힣]+)*$/u', (string)$value)) {
+            $this->errorType = 'charset';
             return false;
         }
+
         return true;
     }
 
@@ -43,6 +55,10 @@ class AllowTagString implements Rule
      */
     public function message()
     {
-        return ':attribute 은(는) 콤마(,)로 구분하여 최대 10개까지 허용됩니다.';
+        return match ($this->errorType) {
+            'max' => ':attribute 은(는) 콤마(,)로 구분하여 최대 10개까지만 입력할 수 있습니다.',
+            'charset' => ':attribute 은(는) 숫자, 영문, 한글만 입력할 수 있으며 공백 및 특수문자는 허용되지 않습니다.',
+            default => ':attribute 형식이 올바르지 않습니다.',
+        };
     }
 }