Browse Source

1.0.2版本

xaboy 7 years ago
parent
commit
80b4959134

+ 15 - 4
src/Form.php

@@ -57,6 +57,8 @@ class Form
 
     protected $components = [];
 
+    protected $fields = [];
+
     protected $script = [];
 
     protected $successScript = '';
@@ -92,7 +94,9 @@ class Form
      */
     public function __construct($action, array $components = [])
     {
-        $this->components = $components;
+        foreach ($components as $component){
+            $this->append($component);
+        }
         $this->action = $action;
         $config = require_once 'config' . DIRECTORY_SEPARATOR . 'config.php';
         $this->setSuccessScript($config['formSuccessScript']);
@@ -220,7 +224,10 @@ class Form
      */
     public function append(FormComponentDriver $component)
     {
-        $this->components[] = $component;
+        $field = $component->getField();
+        if(!isset($this->components[$field]))
+            $this->fields[] = $field;
+        $this->components[$field] = $component;
         return $this;
     }
 
@@ -231,7 +238,10 @@ class Form
      */
     public function prepend(FormComponentDriver $component)
     {
-        array_unshift($this->components, $component);
+        $field = $component->getField();
+        if(!isset($this->components[$field]))
+            array_unshift($this->fields, $field);
+        $this->components[$field] = $component;
         return $this;
     }
 
@@ -242,7 +252,8 @@ class Form
     public function getRules()
     {
         $rules = [];
-        foreach ($this->components as $component) {
+        foreach ($this->fields as $field) {
+            $component = $this->components[$field];
             if (!($component instanceof FormComponentDriver))
                 continue;
 

+ 27 - 0
src/FormComponentDriver.php

@@ -137,6 +137,33 @@ abstract class FormComponentDriver implements FormComponentInterFace
     }
 
     /**
+     * 获取组件的值
+     * @return string
+     */
+    public function getValue()
+    {
+        return $this->value;
+    }
+
+    /**
+     * 获取组件的字段名
+     * @return String
+     */
+    public function getField()
+    {
+        return $this->field;
+    }
+
+    /**
+     * 设置组件的昵称
+     * @return String
+     */
+    public function getTitle()
+    {
+        return $this->title;
+    }
+
+    /**
      * 设置组件的值为必填
      * @param null $message
      * @return $this

+ 4 - 0
src/components/Checkbox.php

@@ -55,6 +55,10 @@ class Checkbox extends FormComponentDriver
         foreach ($this->options as $option){
             $options[] = $option->build();
         }
+        $value = array_unique($this->value);
+        foreach ($value as $k=>$v){
+            $value[$k] = (string)$v;
+        }
         return [
             'type'=>$this->name,
             'field'=>$this->field,

+ 11 - 0
src/components/Frame.php

@@ -9,6 +9,7 @@ namespace FormBuilder\components;
 
 
 use FormBuilder\FormComponentDriver;
+use FormBuilder\Helper;
 
 /**
  * 框架组件
@@ -53,6 +54,16 @@ class Frame extends FormComponentDriver
         'allowRemove' => 'boolean',
     ];
 
+    /**
+     * @param string|array $value
+     * @return $this
+     */
+    public function value($value)
+    {
+        $this->value = $value;
+        return $this;
+    }
+
     protected function init()
     {
         $this->frameTitle('请选择' . $this->title);

+ 10 - 3
src/components/Select.php

@@ -87,9 +87,16 @@ class Select extends FormComponentDriver
             if ($option instanceof Option)
                 $options[] = $option->build();
         }
-        $value = array_unique($this->value);
-        if ($this->props['multiple'] != true)
-            $value = is_array($value) && isset($value[0]) ? $value[0] : '';
+        $value = $this->value;
+        $isArr = is_array($value);
+        if ($this->props['multiple'] == false && $isArr)
+            $value = isset($value[0]) ? $value[0] : '';
+        else if($isArr){
+            $value = array_unique($value);
+            foreach ($value as $k=>$v){
+                $value[$k] = (string)$v;
+            }
+        }
         return [
             'type' => $this->name,
             'field' => $this->field,

+ 7 - 0
src/components/Slider.php

@@ -9,6 +9,7 @@ namespace FormBuilder\components;
 
 
 use FormBuilder\FormComponentDriver;
+use FormBuilder\Helper;
 
 /**
  * 滑块组件
@@ -45,6 +46,12 @@ class Slider extends FormComponentDriver
         'inputSize' => 'string',
     ];
 
+    public function value($value)
+    {
+        $this->value = $value;
+        return $this;
+    }
+
     public function build()
     {
         $value = $this->value;

+ 0 - 2
src/components/Upload.php

@@ -105,8 +105,6 @@ class Upload extends FormComponentDriver
      */
     public function value($value)
     {
-        if ($value === null) $value = '';
-        Helper::verifyType($value, ['array', 'string']);
         $this->value = $value;
         return $this;
     }

+ 16 - 0
src/traits/component/ComponentOptionsTrait.php

@@ -47,4 +47,20 @@ trait ComponentOptionsTrait
         }
         return $this;
     }
+
+    /**
+     * 批量设置选项 支持匿名函数
+     * @param $options
+     * @param bool $disabled
+     * @return $this
+     */
+    public function setOptions($options, $disabled = false)
+    {
+        if(is_callable($options))
+            return $this->setOptions($options($this),$disabled);
+        else if(is_array($options))
+            return $this->options($options,$disabled);
+        else
+            return $this;
+    }
 }

+ 11 - 6
src/traits/form/FormCascaderTrait.php

@@ -29,25 +29,30 @@ trait FormCascaderTrait
 
 
     /**
+     * 省市二级联动
      * @param $field
      * @param $title
-     * @param array $value
+     * @param string $province
+     * @param string $city
      * @return Cascader
      */
-    public static function city($field, $title, array $value = [])
+    public static function city($field, $title, $province = '', $city = '')
     {
-        return self::cascader($field, $title, $value, Cascader::TYPE_CITY);
+        return self::cascader($field, $title, [$province, $city], Cascader::TYPE_CITY);
     }
 
+
     /**
      * 省市区三级联动
      * @param $field
      * @param $title
-     * @param array $value
+     * @param string $province
+     * @param string $city
+     * @param string $area
      * @return Cascader
      */
-    public static function cityArea($field, $title, array $value = [])
+    public static function cityArea($field, $title, $province = '', $city = '', $area = '')
     {
-        return self::cascader($field, $title, $value, Cascader::TYPE_CITY_AREA);
+        return self::cascader($field, $title, [$province, $city, $area], Cascader::TYPE_CITY_AREA);
     }
 }

+ 1 - 1
src/traits/form/FormRadioTrait.php

@@ -14,6 +14,6 @@ trait FormRadioTrait
 {
     public static function radio($field, $title, $value = '')
     {
-        return new Radio($field, $title, $value);
+        return new Radio($field, $title, (string)$value);
     }
 }

+ 5 - 0
src/traits/form/FormSelectTrait.php

@@ -24,4 +24,9 @@ trait FormSelectTrait
     {
         return self::select($field, $title, $value);
     }
+
+    public static function selectOne($field, $title, $value = '')
+    {
+        return self::select($field, $title, (string)$value);
+    }
 }

+ 6 - 3
src/traits/form/FormUploadTrait.php

@@ -22,7 +22,10 @@ trait FormUploadTrait
 
     public static function uploadImages($field, $title, $action, array $value = [])
     {
-        return self::upload($field, $title, $action, $value, Upload::TYPE_IMAGE);
+        $upload = self::upload($field, $title, $action, $value, Upload::TYPE_IMAGE);
+        $upload->format(['jpg','jpeg','png','gif'])->accept('image/*');
+        return $upload;
+
     }
 
     public static function uploadFiles($field, $title, $action, array $value = [])
@@ -32,8 +35,8 @@ trait FormUploadTrait
 
     public static function uploadImageOne($field, $title, $action, $value = '')
     {
-        $upload = self::upload($field, $title, $action, (string)$value, Upload::TYPE_IMAGE);
-        $upload->maxLength(1);
+        $upload = self::upload($field, $title, $action, $value, Upload::TYPE_IMAGE);
+        $upload->format(['jpg','jpeg','png','gif'])->accept('image/*')->maxLength(1);
         return $upload;
     }