فهرست منبع

增加Form::validationErrorToastr方法控制是否使用Toastr展示字段验证错误信息

jqh 4 سال پیش
والد
کامیت
da3be65b59
4فایلهای تغییر یافته به همراه48 افزوده شده و 0 حذف شده
  1. 6 0
      resources/assets/dcat/js/extensions/Form.js
  2. 19 0
      src/Form.php
  3. 2 0
      src/Form/Builder.php
  4. 21 0
      src/Widgets/Form.php

+ 6 - 0
resources/assets/dcat/js/extensions/Form.js

@@ -16,6 +16,8 @@ class Form {
             validate: false,
             // 确认弹窗
             confirm: {title: null, content: null},
+            // 是否使用Toastr展示字段验证错误信息
+            validationErrorToastr: false,
             // 表单错误信息class
             errorClass: 'has-error',
             // 表单错误信息容器选择器
@@ -171,6 +173,10 @@ class Form {
                         _this.options.errorTemplate.replace('{message}', msg[j])
                     );
                 }
+
+                if (_this.options.validationErrorToastr) {
+                    Dcat.error(msg.join('<br/>'));
+                }
             };
 
         queryTabTitleError(_this, $field).removeClass('d-none');

+ 19 - 0
src/Form.php

@@ -268,6 +268,11 @@ class Form implements Renderable
      */
     public $context = [];
 
+    /**
+     * @var bool
+     */
+    public $validationErrorToastr = true;
+
     /**
      * Create a new form instance.
      *
@@ -440,6 +445,20 @@ class Form implements Renderable
         return $this->ajax === true;
     }
 
+    /**
+     * 设置使用 Toastr 展示字段验证信息.
+     *
+     * @param bool $value
+     *
+     * @return $this
+     */
+    public function validationErrorToastr(bool $value = true)
+    {
+        $this->validationErrorToastr = $value;
+
+        return $this;
+    }
+
     /**
      * @param \Closure $closure
      *

+ 2 - 0
src/Form/Builder.php

@@ -830,12 +830,14 @@ EOF;
     protected function addSubmitScript()
     {
         $confirm = admin_javascript_json($this->confirm);
+        $toastr = $this->form->validationErrorToastr ? 'true' : 'false';
 
         Admin::script(
             <<<JS
 $('#{$this->getElementId()}').form({
     validate: true,
     confirm: {$confirm},
+    validationErrorToastr: $toastr,
 });
 JS
         );

+ 21 - 0
src/Widgets/Form.php

@@ -163,6 +163,11 @@ class Form implements Renderable
      */
     protected $confirm = [];
 
+    /**
+     * @var bool
+     */
+    protected $validationErrorToastr = true;
+
     /**
      * Form constructor.
      *
@@ -268,6 +273,20 @@ class Form implements Renderable
         return $this;
     }
 
+    /**
+     * 设置使用 Toastr 展示字段验证信息.
+     *
+     * @param bool $value
+     *
+     * @return $this
+     */
+    public function validationErrorToastr(bool $value = true)
+    {
+        $this->validationErrorToastr = $value;
+
+        return $this;
+    }
+
     /**
      * Set primary key.
      *
@@ -875,12 +894,14 @@ HTML;
     protected function addAjaxScript()
     {
         $confirm = admin_javascript_json($this->confirm);
+        $toastr = $this->validationErrorToastr ? 'true' : 'false';
 
         Admin::script(
             <<<JS
 $('#{$this->getElementId()}').form({
     validate: true,
     confirm: {$confirm},
+    validationErrorToastr: $toastr,
     success: function (data) {
         {$this->savedScript()}
     },