Browse Source

form validator

Jiang qinghua 6 năm trước cách đây
mục cha
commit
66ac436514

+ 279 - 0
src/Form/Concerns/FieldValidator.php

@@ -0,0 +1,279 @@
+<?php
+
+namespace Dcat\Admin\Form\Concerns;
+
+use Dcat\Admin\Form;
+use Illuminate\Support\Arr;
+use Illuminate\Support\Facades\Validator;
+
+/**
+ * @property Form $form
+ */
+trait FieldValidator
+{
+    /**
+     * The validation rules for creation.
+     *
+     * @var array|\Closure
+     */
+    protected $creationRules = [];
+
+    /**
+     * The validation rules for updates.
+     *
+     * @var array|\Closure
+     */
+    protected $updateRules = [];
+
+    /**
+     * Validation rules.
+     *
+     * @var string|\Closure
+     */
+    protected $rules = '';
+
+    /**
+     * @var \Closure
+     */
+    protected $validator;
+
+    /**
+     * Validation messages.
+     *
+     * @var array
+     */
+    protected $validationMessages = [];
+
+    /**
+     * Set the update validation rules for the field.
+     *
+     * @param array|callable|string $rules
+     * @param array $messages
+     *
+     * @return $this
+     */
+    public function updateRules($rules = null, $messages = [])
+    {
+        $this->updateRules = $this->mergeRules($rules, $this->updateRules);
+
+        $this->setValidationMessages('update', $messages);
+
+        return $this;
+    }
+
+    /**
+     * Set the creation validation rules for the field.
+     *
+     * @param array|callable|string $rules
+     * @param array $messages
+     *
+     * @return $this
+     */
+    public function creationRules($rules = null, $messages = [])
+    {
+        $this->creationRules = $this->mergeRules($rules, $this->creationRules);
+
+        $this->setValidationMessages('creation', $messages);
+
+        return $this;
+    }
+
+    /**
+     * Get or set rules.
+     *
+     * @param null $rules
+     * @param array $messages
+     *
+     * @return $this
+     */
+    public function rules($rules = null, $messages = [])
+    {
+        if ($rules instanceof \Closure) {
+            $this->rules = $rules;
+        }
+
+        if (is_array($rules)) {
+            $thisRuleArr = array_filter(explode('|', $this->rules));
+
+            $this->rules = array_merge($thisRuleArr, $rules);
+        } elseif (is_string($rules)) {
+            $rules = array_filter(explode('|', "{$this->rules}|$rules"));
+
+            $this->rules = implode('|', $rules);
+        }
+
+        $this->setValidationMessages('default', $messages);
+
+        return $this;
+    }
+
+    /**
+     * Get field validation rules.
+     *
+     * @return string
+     */
+    protected function getRules()
+    {
+        if (request()->isMethod('POST')) {
+            $rules = $this->creationRules ?: $this->rules;
+        } elseif (request()->isMethod('PUT')) {
+            $rules = $this->updateRules ?: $this->rules;
+        } else {
+            $rules = $this->rules;
+        }
+
+        if ($rules instanceof \Closure) {
+            $rules = $rules->call($this, $this->form);
+        }
+
+        if (is_string($rules)) {
+            $rules = array_filter(explode('|', $rules));
+        }
+
+        if (!$this->form) {
+            return $rules;
+        }
+
+        if (!$id = $this->form->getKey()) {
+            return $rules;
+        }
+
+        if (is_array($rules)) {
+            foreach ($rules as &$rule) {
+                if (is_string($rule)) {
+                    $rule = str_replace('{{id}}', $id, $rule);
+                }
+            }
+        }
+
+        return $rules;
+    }
+
+    /**
+     * Remove a specific rule by keyword.
+     *
+     * @param string $rule
+     *
+     * @return void
+     */
+    public function removeRule($rule)
+    {
+        if (!$this->rules || !is_string($this->rules)) {
+            return;
+        }
+
+        $pattern = "/{$rule}[^\|]?(\||$)/";
+        $this->rules = preg_replace($pattern, '', $this->rules, -1);
+    }
+
+    /**
+     * @param string $rule
+     *
+     * @return bool
+     */
+    public function hasRule($rule)
+    {
+        if (!$this->rules || !is_string($this->rules)) {
+            return false;
+        }
+
+        $pattern = "/{$rule}[^\|]?(\||$)/";
+
+        return preg_match($pattern, $this->rules);
+    }
+
+    /**
+     * Set field validator.
+     *
+     * @param callable $validator
+     *
+     * @return $this
+     */
+    public function validator(callable $validator)
+    {
+        $this->validator = $validator;
+
+        return $this;
+    }
+
+    /**
+     * Get validator for this field.
+     *
+     * @param array $input
+     *
+     * @return bool|Validator
+     */
+    public function getValidator(array $input)
+    {
+        if ($this->validator) {
+            return $this->validator->call($this, $input);
+        }
+
+        $rules = $attributes = [];
+
+        if (!$fieldRules = $this->getRules()) {
+            return false;
+        }
+
+        if (is_string($this->column)) {
+            if (!Arr::has($input, $this->column)) {
+                return false;
+            }
+
+            $input = $this->sanitizeInput($input, $this->column);
+
+            $rules[$this->column] = $fieldRules;
+            $attributes[$this->column] = $this->label;
+        }
+
+        if (is_array($this->column)) {
+            foreach ($this->column as $key => $column) {
+                if (!array_key_exists($column, $input)) {
+                    continue;
+                }
+                $input[$column . $key] = Arr::get($input, $column);
+                $rules[$column . $key] = $fieldRules;
+                $attributes[$column . $key] = $this->label . "[$column]";
+            }
+        }
+
+        return Validator::make($input, $rules, $this->getValidationMessages(), $attributes);
+    }
+
+
+    /**
+     * Set validation messages for column.
+     *
+     * @param string $key
+     * @param array $messages
+     *
+     * @return $this
+     */
+    public function setValidationMessages($key, array $messages)
+    {
+        $this->validationMessages[$key] = $messages;
+
+        return $this;
+    }
+
+    /**
+     * Get validation messages for the field.
+     *
+     * @return array|mixed
+     */
+    public function getValidationMessages()
+    {
+        // Default validation message.
+        $messages = $this->validationMessages['default'] ?? [];
+
+        if (request()->isMethod('POST')) {
+            $messages = $this->validationMessages['creation'] ?? $messages;
+        } elseif (request()->isMethod('PUT')) {
+            $messages = $this->validationMessages['update'] ?? $messages;
+        }
+
+        return $messages;
+    }
+
+
+}

+ 2 - 155
src/Form/Field.php

@@ -4,10 +4,10 @@ namespace Dcat\Admin\Form;
 
 
 use Dcat\Admin\Admin;
 use Dcat\Admin\Admin;
 use Dcat\Admin\Form;
 use Dcat\Admin\Form;
+use Dcat\Admin\Form\Concerns;
 use Illuminate\Contracts\Support\Arrayable;
 use Illuminate\Contracts\Support\Arrayable;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Arr;
-use Illuminate\Support\Facades\Validator;
 use Illuminate\Support\Fluent;
 use Illuminate\Support\Fluent;
 use Illuminate\Support\Traits\Macroable;
 use Illuminate\Support\Traits\Macroable;
 
 
@@ -16,7 +16,7 @@ use Illuminate\Support\Traits\Macroable;
  */
  */
 class Field implements Renderable
 class Field implements Renderable
 {
 {
-    use Macroable;
+    use Macroable, Concerns\FieldValidator;
 
 
     const FILE_DELETE_FLAG = '_file_del_';
     const FILE_DELETE_FLAG = '_file_del_';
 
 
@@ -104,25 +104,6 @@ class Field implements Renderable
      */
      */
     protected $checked = [];
     protected $checked = [];
 
 
-    /**
-     * Validation rules.
-     *
-     * @var string|\Closure
-     */
-    protected $rules = '';
-
-    /**
-     * @var callable
-     */
-    protected $validator;
-
-    /**
-     * Validation messages.
-     *
-     * @var array
-     */
-    protected $validationMessages = [];
-
     /**
     /**
      * Css required by this field.
      * Css required by this field.
      *
      *
@@ -487,96 +468,6 @@ class Field implements Renderable
         return $this;
         return $this;
     }
     }
 
 
-    /**
-     * Get or set rules.
-     *
-     * @param null  $rules
-     * @param array $messages
-     *
-     * @return $this
-     */
-    public function rules($rules = null, $messages = [])
-    {
-        if ($rules instanceof \Closure) {
-            $this->rules = $rules;
-        }
-
-        if (is_array($rules)) {
-            $thisRuleArr = array_filter(explode('|', $this->rules));
-
-            $this->rules = array_merge($thisRuleArr, $rules);
-        } elseif (is_string($rules)) {
-            $rules = array_filter(explode('|', "{$this->rules}|$rules"));
-
-            $this->rules = implode('|', $rules);
-        }
-
-        $this->validationMessages = $messages;
-
-        return $this;
-    }
-
-    /**
-     * Get field validation rules.
-     *
-     * @return string
-     */
-    public function getRules()
-    {
-        if ($this->rules instanceof \Closure) {
-            return $this->rules->call($this, $this->form);
-        }
-
-        return $this->rules;
-    }
-
-    /**
-     * Remove a specific rule by keyword.
-     *
-     * @param string $rule
-     *
-     * @return void
-     */
-    public function removeRule($rule)
-    {
-        if (!$this->rules || !is_string($this->rules)) {
-            return;
-        }
-
-        $pattern = "/{$rule}[^\|]?(\||$)/";
-        $this->rules = preg_replace($pattern, '', $this->rules, -1);
-    }
-
-    /**
-     * @param string $rule
-     *
-     * @return bool
-     */
-    public function hasRule($rule)
-    {
-        if (!$this->rules || !is_string($this->rules)) {
-            return false;
-        }
-
-        $pattern = "/{$rule}[^\|]?(\||$)/";
-
-        return preg_match($pattern, $this->rules);
-    }
-
-    /**
-     * Set field validator.
-     *
-     * @param callable $validator
-     *
-     * @return $this
-     */
-    public function validator(callable $validator)
-    {
-        $this->validator = $validator;
-
-        return $this;
-    }
-
     /**
     /**
      * Get key for error message.
      * Get key for error message.
      *
      *
@@ -721,50 +612,6 @@ class Field implements Renderable
         return $this->original;
         return $this->original;
     }
     }
 
 
-    /**
-     * Get validator for this field.
-     *
-     * @param array $input
-     *
-     * @return bool|Validator
-     */
-    public function getValidator(array $input)
-    {
-        if ($this->validator) {
-            return $this->validator->call($this, $input);
-        }
-
-        $rules = $attributes = [];
-
-        if (!$fieldRules = $this->getRules()) {
-            return false;
-        }
-
-        if (is_string($this->column)) {
-            if (!Arr::has($input, $this->column)) {
-                return false;
-            }
-
-            $input = $this->sanitizeInput($input, $this->column);
-
-            $rules[$this->column] = $fieldRules;
-            $attributes[$this->column] = $this->label;
-        }
-
-        if (is_array($this->column)) {
-            foreach ($this->column as $key => $column) {
-                if (!array_key_exists($column, $input)) {
-                    continue;
-                }
-                $input[$column.$key] = Arr::get($input, $column);
-                $rules[$column.$key] = $fieldRules;
-                $attributes[$column.$key] = $this->label."[$column]";
-            }
-        }
-
-        return Validator::make($input, $rules, $this->validationMessages, $attributes);
-    }
-
     /**
     /**
      * Sanitize input data.
      * Sanitize input data.
      *
      *

+ 1 - 1
src/Form/Field/BootstrapFile.php

@@ -90,7 +90,7 @@ class BootstrapFile extends Field
         $rules[$this->column] = $fieldRules;
         $rules[$this->column] = $fieldRules;
         $attributes[$this->column] = $this->label;
         $attributes[$this->column] = $this->label;
 
 
-        return Validator::make($input, $rules, $this->validationMessages, $attributes);
+        return Validator::make($input, $rules, $this->getValidationMessages(), $attributes);
     }
     }
 
 
     /**
     /**

+ 1 - 1
src/Form/Field/BootstrapMultipleFile.php

@@ -76,7 +76,7 @@ class BootstrapMultipleFile extends Field
 
 
         list($rules, $input) = $this->hydrateFiles(Arr::get($input, $this->column, []));
         list($rules, $input) = $this->hydrateFiles(Arr::get($input, $this->column, []));
 
 
-        return Validator::make($input, $rules, $this->validationMessages, $attributes);
+        return Validator::make($input, $rules, $this->getValidationMessages(), $attributes);
     }
     }
 
 
     /**
     /**

+ 1 - 1
src/Form/Field/Embeds.php

@@ -129,7 +129,7 @@ class Embeds extends Field
             return false;
             return false;
         }
         }
 
 
-        return Validator::make($input, $rules, $this->validationMessages, $attributes);
+        return Validator::make($input, $rules, $this->getValidationMessages(), $attributes);
     }
     }
 
 
     /**
     /**

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

@@ -90,7 +90,7 @@ class File extends Field
         $rules[$this->column] = 'required';
         $rules[$this->column] = 'required';
         $attributes[$this->column] = $this->label;
         $attributes[$this->column] = $this->label;
 
 
-        return Validator::make($input, $rules, $this->validationMessages, $attributes);
+        return Validator::make($input, $rules, $this->getValidationMessages(), $attributes);
     }
     }
 
 
     /**
     /**

+ 1 - 1
src/Form/Field/HasMany.php

@@ -165,7 +165,7 @@ class HasMany extends Field
             $newInput = $input;
             $newInput = $input;
         }
         }
 
 
-        return Validator::make($newInput, $newRules, $this->validationMessages, $attributes);
+        return Validator::make($newInput, $newRules, $this->getValidationMessages(), $attributes);
     }
     }
 
 
     /**
     /**