浏览代码

Add Form\Text::minLength and Form\Text::maxLength

jqh 5 年之前
父节点
当前提交
af4671e3ed
共有 3 个文件被更改,包括 60 次插入1 次删除
  1. 2 0
      src/Controllers/AuthController.php
  2. 6 1
      src/Controllers/UserController.php
  3. 52 0
      src/Form/Field/Text.php

+ 2 - 0
src/Controllers/AuthController.php

@@ -170,6 +170,8 @@ class AuthController extends Controller
         $form->password('old_password', trans('admin.old_password'));
 
         $form->password('password', trans('admin.password'))
+            ->minLength(5)
+            ->maxLength(20)
             ->customFormat(function ($v) {
                 if ($v == $this->password) {
                     return;

+ 6 - 1
src/Controllers/UserController.php

@@ -243,6 +243,8 @@ class UserController extends Controller
 
             if ($id) {
                 $form->password('password', trans('admin.password'))
+                    ->minLength(5)
+                    ->maxLength(20)
                     ->customFormat(function ($v) {
                         if ($v == $this->password) {
                             return;
@@ -250,7 +252,10 @@ class UserController extends Controller
                         return $v;
                     });
             } else {
-                $form->password('password', trans('admin.password'))->required();
+                $form->password('password', trans('admin.password'))
+                    ->required()
+                    ->minLength(5)
+                    ->maxLength(20);
             }
 
             $form->password('password_confirmation', trans('admin.password_confirmation'))->confirm('password');

+ 52 - 0
src/Form/Field/Text.php

@@ -2,6 +2,7 @@
 
 namespace Dcat\Admin\Form\Field;
 
+use Dcat\Admin\Admin;
 use Dcat\Admin\Form\Field;
 
 class Text extends Field
@@ -71,6 +72,57 @@ class Text extends Field
         return $this->attribute($attributes);
     }
 
+    /**
+     * @param int $length
+     * @param string|null $error
+     * @return $this
+     */
+    public function minLength(int $length, ?string $error = null)
+    {
+        $this->rules('min:'.$length);
+
+        $defaultError = trans('validation.min.string') ?: 'The :attribute may not be greater than :max characters.';
+
+        return $this->attribute([
+            'data-minlength'       => $length,
+            'data-minlength-error' => str_replace(
+                [':attribute', ':min'],
+                [$this->column, $length],
+                $error ?: $defaultError
+            ),
+        ]);
+    }
+
+    /**
+     * @param int $length
+     * @param string|null $error
+     * @return $this
+     */
+    public function maxLength(int $length, ?string $error = null)
+    {
+        Admin::script(
+            <<<'JS'
+$.fn.validator.Constructor.DEFAULTS.custom.maxlength = function ($el) {
+    var length = $el.attr('data-maxlength');
+    return $el.val().length > length;
+};
+JS
+        );
+
+        $this->rules('max:'.$length);
+
+        $defaultError = trans('validation.max.string') ?: 'The :attribute may not be greater than :max characters.';
+
+        return $this->attribute([
+            'data-maxlength'       => $length,
+            'data-maxlength-error' => str_replace(
+                [':attribute', ':max'],
+                [$this->column, $length],
+                $error ?: $defaultError
+            ),
+        ]);
+    }
+
     /**
      * Set error messages for individual form field.
      *