Browse Source

文件上传表单支持required_if验证规则

jqh 4 years ago
parent
commit
57bb0c7bea
3 changed files with 59 additions and 8 deletions
  1. 47 6
      src/Form/Concerns/HasFieldValidator.php
  2. 10 0
      src/Form/Field.php
  3. 2 2
      src/Form/Field/File.php

+ 47 - 6
src/Form/Concerns/HasFieldValidator.php

@@ -115,9 +115,9 @@ trait HasFieldValidator
      */
     protected function getRules()
     {
-        if (request()->isMethod('POST')) {
+        if ($this->isCreating()) {
             $rules = $this->creationRules ?: $this->rules;
-        } elseif (request()->isMethod('PUT')) {
+        } elseif ($this->isEditing()) {
             $rules = $this->updateRules ?: $this->rules;
         } else {
             $rules = $this->rules;
@@ -274,9 +274,42 @@ trait HasFieldValidator
      */
     public function hasRule($rule)
     {
-        return $this->isRuleExists($this->rules, $rule);
+        return $this->isRuleExists($this->getRules(), $rule);
     }
 
+    /**
+     * @param string $rule
+     *
+     * @return bool|mixed
+     */
+    protected function getRule($rule)
+    {
+        $rules = $this->getRules();
+
+        if (is_array($rules)) {
+            foreach ($rules as $r) {
+                if ($this->isRuleExists($r, $rule)) {
+                    return $r;
+                }
+            }
+
+            return false;
+        }
+
+        if (! is_string($rules)) {
+            return false;
+        }
+
+        foreach (explode('|', $rules) as $r) {
+            if ($this->isRuleExists($r, $rule)) {
+                return $r;
+            }
+        }
+
+        return false;
+    }
+
+
     /**
      * @param $rules
      * @param $rule
@@ -286,13 +319,21 @@ trait HasFieldValidator
     protected function isRuleExists($rules, $rule)
     {
         if (is_array($rules)) {
-            return in_array($rule, $rules);
+            foreach ($rules as $r) {
+                if ($this->isRuleExists($r, $rule)) {
+                    return true;
+                }
+            }
+
+            return false;
         }
 
         if (! is_string($rules)) {
             return false;
         }
 
+        $rule = str_replace(['*', '/'], ['([0-9a-z-_,:=><])*', "\/"], $rule);
+
         $pattern = "/{$rule}[^\|]?(\||$)/";
 
         return (bool) preg_match($pattern, $rules);
@@ -381,9 +422,9 @@ trait HasFieldValidator
         // Default validation message.
         $messages = $this->validationMessages['default'] ?? [];
 
-        if (request()->isMethod('POST')) {
+        if ($this->isCreating()) {
             $messages = $this->validationMessages['creation'] ?? $messages;
-        } elseif (request()->isMethod('PUT')) {
+        } elseif ($this->isEditing()) {
             $messages = $this->validationMessages['update'] ?? $messages;
         }
 

+ 10 - 0
src/Form/Field.php

@@ -1117,6 +1117,16 @@ class Field implements Renderable
         ]);
     }
 
+    protected function isCreating()
+    {
+        return request()->isMethod('POST');
+    }
+
+    protected function isEditing()
+    {
+        return request()->isMethod('PUT');
+    }
+
     /**
      * Get view of this field.
      *

+ 2 - 2
src/Form/Field/File.php

@@ -78,11 +78,11 @@ class File extends Field implements UploadFieldInterface
 
         $rules = $attributes = [];
 
-        if (! $this->hasRule('required')) {
+        if (! $this->hasRule('required') && ! $requiredIf = $this->getRule('required_if*')) {
             return false;
         }
 
-        $rules[$this->column] = 'required';
+        $rules[$this->column] = $requiredIf ?: 'required';
         $attributes[$this->column] = $this->label;
 
         return Validator::make($input, $rules, $this->getValidationMessages(), $attributes);