|
|
@@ -11,6 +11,7 @@ use FormBuilder\components\Cascader;
|
|
|
use FormBuilder\components\FormStyle;
|
|
|
use FormBuilder\components\Hidden;
|
|
|
use FormBuilder\components\Row;
|
|
|
+use FormBuilder\exception\FormBuilderException;
|
|
|
use FormBuilder\traits\form\FormCascaderTrait;
|
|
|
use FormBuilder\traits\form\FormCheckBoxTrait;
|
|
|
use FormBuilder\traits\form\FormColorPickerTrait;
|
|
|
@@ -33,6 +34,7 @@ use FormBuilder\traits\form\FormValidateTrait;
|
|
|
|
|
|
/**
|
|
|
* Class Form
|
|
|
+ *
|
|
|
* @package FormBuilder
|
|
|
*/
|
|
|
class Form
|
|
|
@@ -59,12 +61,14 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 三级联动 加载省市数据
|
|
|
+ *
|
|
|
* @var bool
|
|
|
*/
|
|
|
protected $loadCityData = false;
|
|
|
|
|
|
/**
|
|
|
* 三级联动 加载省市区数据
|
|
|
+ *
|
|
|
* @var bool
|
|
|
*/
|
|
|
protected $loadCityAreaData = false;
|
|
|
@@ -96,18 +100,21 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 加载 jquery
|
|
|
+ *
|
|
|
* @var bool
|
|
|
*/
|
|
|
protected $linkJq = true;
|
|
|
|
|
|
/**
|
|
|
* 加载 vue
|
|
|
+ *
|
|
|
* @var bool
|
|
|
*/
|
|
|
protected $linkVue = true;
|
|
|
|
|
|
/**
|
|
|
* 加载 iview
|
|
|
+ *
|
|
|
* @var bool
|
|
|
*/
|
|
|
protected $linkIview = true;
|
|
|
@@ -119,24 +126,28 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 网页标题
|
|
|
+ *
|
|
|
* @var string
|
|
|
*/
|
|
|
protected $title = 'formBuilder';
|
|
|
|
|
|
/**
|
|
|
* 提交地址
|
|
|
+ *
|
|
|
* @var string
|
|
|
*/
|
|
|
protected $action = '';
|
|
|
|
|
|
/**
|
|
|
* 表单id
|
|
|
+ *
|
|
|
* @var string
|
|
|
*/
|
|
|
protected $id = '';
|
|
|
|
|
|
/**
|
|
|
* 提交方式
|
|
|
+ *
|
|
|
* @var string
|
|
|
*/
|
|
|
protected $method = 'post';
|
|
|
@@ -147,6 +158,7 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 表单配置
|
|
|
+ *
|
|
|
* @var array|mixed
|
|
|
*/
|
|
|
protected $config = [
|
|
|
@@ -162,8 +174,9 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* Form constructor.
|
|
|
+ *
|
|
|
* @param string $action 提交地址
|
|
|
- * @param array $components 组件
|
|
|
+ * @param array $components 组件
|
|
|
*/
|
|
|
public function __construct($action = '', array $components = [])
|
|
|
{
|
|
|
@@ -171,6 +184,11 @@ class Form
|
|
|
$this->action = $action;
|
|
|
}
|
|
|
|
|
|
+ public static function json()
|
|
|
+ {
|
|
|
+ return new Json();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @param bool $linkJq
|
|
|
*/
|
|
|
@@ -217,7 +235,7 @@ class Form
|
|
|
*/
|
|
|
public function components(array $components = [])
|
|
|
{
|
|
|
- foreach ($components as $component){
|
|
|
+ foreach ($components as $component) {
|
|
|
$this->append($component);
|
|
|
}
|
|
|
return $this;
|
|
|
@@ -254,6 +272,7 @@ class Form
|
|
|
/**
|
|
|
* 表单提交后成功执行的js地址
|
|
|
* formCreate.formSuccess(formData,$f)
|
|
|
+ *
|
|
|
* @param string $successScript
|
|
|
* @return $this
|
|
|
*/
|
|
|
@@ -292,6 +311,7 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 提交地址
|
|
|
+ *
|
|
|
* @param string $action
|
|
|
* @return $this
|
|
|
*/
|
|
|
@@ -315,7 +335,7 @@ class Form
|
|
|
*/
|
|
|
public function getConfig($key = '')
|
|
|
{
|
|
|
- if($key =='')
|
|
|
+ if ($key == '')
|
|
|
return $this->config;
|
|
|
else
|
|
|
return isset($this->config[$key]) ? $this->config[$key] : null;
|
|
|
@@ -323,6 +343,7 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 提交方式
|
|
|
+ *
|
|
|
* @param string $method
|
|
|
* @return $this
|
|
|
*/
|
|
|
@@ -334,6 +355,7 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 标题
|
|
|
+ *
|
|
|
* @return string
|
|
|
*/
|
|
|
public function getTitle()
|
|
|
@@ -354,13 +376,14 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 追加组件
|
|
|
+ *
|
|
|
* @param FormComponentDriver $component
|
|
|
* @return $this
|
|
|
*/
|
|
|
public function append(FormComponentDriver $component)
|
|
|
{
|
|
|
$field = $component->getField();
|
|
|
- if(!isset($this->components[$field]))
|
|
|
+ if (!isset($this->components[$field]))
|
|
|
$this->fields[] = $field;
|
|
|
$this->components[$field] = $component;
|
|
|
$this->checkLoadData($component);
|
|
|
@@ -369,13 +392,14 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 开头插入组件
|
|
|
+ *
|
|
|
* @param FormComponentDriver $component
|
|
|
* @return $this
|
|
|
*/
|
|
|
public function prepend(FormComponentDriver $component)
|
|
|
{
|
|
|
$field = $component->getField();
|
|
|
- if(!isset($this->components[$field]))
|
|
|
+ if (!isset($this->components[$field]))
|
|
|
array_unshift($this->fields, $field);
|
|
|
$this->components[$field] = $component;
|
|
|
$this->checkLoadData($component);
|
|
|
@@ -384,14 +408,15 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 是否需要引入省市区数据
|
|
|
+ *
|
|
|
* @param FormComponentDriver $component
|
|
|
*/
|
|
|
protected function checkLoadData(FormComponentDriver $component)
|
|
|
{
|
|
|
- if(
|
|
|
+ if (
|
|
|
$component instanceof Cascader
|
|
|
&& ($this->loadCityData == false || $this->loadCityAreaData == false)
|
|
|
- ){
|
|
|
+ ) {
|
|
|
$type = $component->getType();
|
|
|
if ($type == Cascader::TYPE_CITY)
|
|
|
$this->loadCityData = true;
|
|
|
@@ -402,18 +427,26 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 获得表单规则
|
|
|
+ *
|
|
|
* @return array
|
|
|
+ * @throws FormBuilderException
|
|
|
*/
|
|
|
public function getRules()
|
|
|
{
|
|
|
$rules = [];
|
|
|
+ $fields = [];
|
|
|
foreach ($this->fields as $field) {
|
|
|
$component = $this->components[$field];
|
|
|
if (!($component instanceof FormComponentDriver))
|
|
|
continue;
|
|
|
+ $field = $component->getField();
|
|
|
+ if (in_array($field, $fields))
|
|
|
+ throw new FormBuilderException($field . '字段已重复,请保证组件 field 无重复');
|
|
|
+
|
|
|
+ $fields[] = $field;
|
|
|
$rule = $component->build();
|
|
|
- if(!$component instanceof Hidden)
|
|
|
- $rule['validate'] = array_merge(isset($rule['validate']) ? $rule['validate'] : [],$component->validate()->build());
|
|
|
+ if (!$component instanceof Hidden)
|
|
|
+ $rule['validate'] = array_merge(isset($rule['validate']) ? $rule['validate'] : [], $component->validate()->build());
|
|
|
$rules[] = $rule;
|
|
|
}
|
|
|
return $rules;
|
|
|
@@ -422,6 +455,7 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 获取表单视图
|
|
|
+ *
|
|
|
* @return string
|
|
|
*/
|
|
|
public function view()
|
|
|
@@ -435,6 +469,7 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 获取表单生成器所需全部js
|
|
|
+ *
|
|
|
* @return array
|
|
|
*/
|
|
|
public function script()
|
|
|
@@ -444,6 +479,7 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 获取生成表单的js代码
|
|
|
+ *
|
|
|
* @return string
|
|
|
*/
|
|
|
public function formScript()
|
|
|
@@ -458,6 +494,7 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 获取表单生成器所需js
|
|
|
+ *
|
|
|
* @return array
|
|
|
*/
|
|
|
public function getScript()
|
|
|
@@ -470,19 +507,20 @@ class Form
|
|
|
$script[] = $_script['city-area-data'];
|
|
|
if ($this->loadCityData == true)
|
|
|
$script[] = $_script['city-data'];
|
|
|
- if($this->linkJq)
|
|
|
+ if ($this->linkJq)
|
|
|
$script[] = $_script['jq'];
|
|
|
- if($this->linkIview){
|
|
|
+ if ($this->linkIview) {
|
|
|
$script[] = $_script['iview'];
|
|
|
$script[] = $_script['iview-css'];
|
|
|
}
|
|
|
- if($this->linkVue)
|
|
|
+ if ($this->linkVue)
|
|
|
$script[] = $_script['vue'];
|
|
|
return array_reverse($script);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 是否隐藏提交按钮(默认显示)
|
|
|
+ *
|
|
|
* @param bool $isShow
|
|
|
* @return Form
|
|
|
*/
|
|
|
@@ -495,6 +533,7 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 是否隐藏重置按钮(默认隐藏)
|
|
|
+ *
|
|
|
* @param bool $isShow
|
|
|
* @return Form
|
|
|
*/
|
|
|
@@ -523,12 +562,13 @@ class Form
|
|
|
|
|
|
/**
|
|
|
* 生成表单快捷方法
|
|
|
+ *
|
|
|
* @param string $action
|
|
|
- * @param array $components
|
|
|
+ * @param array $components
|
|
|
* @return Form
|
|
|
*/
|
|
|
public static function create($action, array $components = [])
|
|
|
{
|
|
|
- return new static($action, $components);
|
|
|
+ return new self($action, $components);
|
|
|
}
|
|
|
}
|