Browse Source

Update Core\Validator

Visman 3 years ago
parent
commit
cc4512309e
3 changed files with 31 additions and 7 deletions
  1. 25 7
      app/Core/Validator.php
  2. 3 0
      app/lang/en/validator.po
  3. 3 0
      app/lang/ru/validator.po

+ 25 - 7
app/Core/Validator.php

@@ -113,6 +113,7 @@ class Validator
             'array'         => [$this, 'vArray'],
             'checkbox'      => [$this, 'vCheckbox'],
             'date'          => [$this, 'vDate'],
+            'exist'         => [$this, 'vExist'],
             'file'          => [$this, 'vFile'],
             'image'         => [$this, 'vImage'],
             'in'            => [$this, 'vIn'],
@@ -492,6 +493,15 @@ class Validator
         return $value;
     }
 
+    protected function vExist(Validator $v, /* mixed */ $value) /* : mixed */
+    {
+        if (null === $value) {
+            $this->addError('The :alias not exist');
+        }
+
+        return $value;
+    }
+
     protected function vRequired(Validator $v, /* mixed */ $value) /* : mixed */
     {
         if ($this->noValue($value, true)) {
@@ -537,14 +547,12 @@ class Validator
                     case 'linebreaks':
                         $value = \str_replace(["\r\n", "\r"], "\n", $value);
                         break;
+                    default:
+                        throw new InvalidArgumentException("Bad action: {$action}");
                 }
             }
         } elseif (null === $value) {
-            if (\in_array('trim', $actions, true)) {
-                $value = ''; // string:trim -> отсутвующее поле становится пустым
-            } else {
-                $this->addError(null);
-            }
+            $this->addError(null);
         } else {
             $this->addError('The :alias must be string');
 
@@ -558,8 +566,13 @@ class Validator
     {
         if (\is_numeric($value)) {
             $value += 0;
-        } elseif (null === $value) {
+        } elseif (
+            null === $value
+            || '' === $value
+        ) {
             $this->addError(null);
+
+            $value = null;
         } else {
             $this->addError('The :alias must be numeric');
 
@@ -576,8 +589,13 @@ class Validator
             && \is_int(0 + $value)
         ) {
             $value += 0;
-        } elseif (null === $value) {
+        } elseif (
+            null === $value
+            || '' === $value
+        ) {
             $this->addError(null);
+
+            $value = null;
         } else {
             $this->addError('The :alias must be integer');
 

+ 3 - 0
app/lang/en/validator.po

@@ -191,3 +191,6 @@ msgstr "Failed to determine the date in the :alias field."
 
 msgid "Too much data"
 msgstr "Too much data"
+
+msgid "The :alias not exist"
+msgstr "The :alias field not exist."

+ 3 - 0
app/lang/ru/validator.po

@@ -191,3 +191,6 @@ msgstr "Не удалось определить дату в поле :alias."
 
 msgid "Too much data"
 msgstr "Лишние данные"
+
+msgid "The :alias not exist"
+msgstr "Поле :alias отсутствует."