Selaa lähdekoodia

增加"Form::creating"方法

jqh 6 vuotta sitten
vanhempi
commit
6698499e17
3 muutettua tiedostoa jossa 46 lisäystä ja 4 poistoa
  1. 21 2
      src/Form.php
  2. 2 2
      src/Form/Builder.php
  3. 23 0
      src/Form/Concerns/Events.php

+ 21 - 2
src/Form.php

@@ -315,6 +315,22 @@ class Form implements Renderable
         return $this;
     }
 
+    /**
+     * @return bool
+     */
+    public function isCreating()
+    {
+        return $this->builder->isCreating();
+    }
+
+    /**
+     * @return bool
+     */
+    public function isEditing()
+    {
+        return $this->builder->isEditing();
+    }
+
     /**
      * @return Fluent
      */
@@ -741,7 +757,7 @@ class Form implements Renderable
         }
 
         // 非ajax请求
-        $status = $options['status'] ?? 200;
+        $status = (int) ($options['status_code'] ?? 200);
 
         admin_alert($message);
 
@@ -1400,6 +1416,10 @@ class Form implements Renderable
     public function render()
     {
         try {
+            if ($this->isCreating()) {
+                $this->callCreating();
+            }
+
             $this->callComposing();
 
             return $this->builder->render();
@@ -1427,7 +1447,6 @@ class Form implements Renderable
 
     /**
      * @param string|array $keys
-     * @param null   $value
      *
      * @return void
      */

+ 2 - 2
src/Form/Builder.php

@@ -59,7 +59,7 @@ class Builder
      *
      * @var string
      */
-    protected $mode = 'create';
+    protected $mode = self::MODE_CREATE;
 
     /**
      * @var array
@@ -235,7 +235,7 @@ class Builder
      *
      * @return void
      */
-    public function setMode($mode = 'create')
+    public function setMode(string $mode = self::MODE_CREATE)
     {
         $this->mode = $mode;
     }

+ 23 - 0
src/Form/Concerns/Events.php

@@ -12,6 +12,7 @@ trait Events
      * @var array
      */
     protected $_listeners = [
+        'creating' => [],
         'editing' => [],
         'submitted' => [],
         'saving' => [],
@@ -20,6 +21,18 @@ trait Events
         'deleted' => [],
     ];
 
+    /**
+     * Set after getting creating model callback.
+     *
+     * @param Closure $callback
+     *
+     * @return void
+     */
+    public function creating(Closure $callback)
+    {
+        $this->_listeners['creating'][] = $callback;
+    }
+
     /**
      * Set after getting editing model callback.
      *
@@ -88,6 +101,16 @@ trait Events
         $this->_listeners['deleted'][] = $callback;
     }
 
+    /**
+     * Call creating callbacks.
+     *
+     * @return mixed
+     */
+    protected function callCreating()
+    {
+        return $this->callListeners('creating');
+    }
+
     /**
      * Call editing callbacks.
      *