|
@@ -15,6 +15,7 @@ use Dcat\Admin\Form;
|
|
|
* $formHelper->embedClassFields('user', '用户信息', [User::class]);
|
|
* $formHelper->embedClassFields('user', '用户信息', [User::class]);
|
|
|
*
|
|
*
|
|
|
* 类属性注释示例:
|
|
* 类属性注释示例:
|
|
|
|
|
+ *
|
|
|
* @label 用户名
|
|
* @label 用户名
|
|
|
*/
|
|
*/
|
|
|
class FormHelper
|
|
class FormHelper
|
|
@@ -35,12 +36,13 @@ class FormHelper
|
|
|
public function __construct($form, $controller)
|
|
public function __construct($form, $controller)
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
- $this->form = $form;
|
|
|
|
|
|
|
+ $this->form = $form;
|
|
|
$this->controller = $controller;
|
|
$this->controller = $controller;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function enableBack(){
|
|
|
|
|
- $this->form->tools(function (Form\Tools $tools){
|
|
|
|
|
|
|
+ public function enableBack()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->form->tools(function (Form\Tools $tools) {
|
|
|
$tools->prepend();
|
|
$tools->prepend();
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
@@ -56,23 +58,24 @@ class FormHelper
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 嵌套表单
|
|
* 嵌套表单
|
|
|
|
|
+ *
|
|
|
* @param $field
|
|
* @param $field
|
|
|
* @param $lable
|
|
* @param $lable
|
|
|
* @return Form\Field\Embeds
|
|
* @return Form\Field\Embeds
|
|
|
* @throws \ReflectionException
|
|
* @throws \ReflectionException
|
|
|
*/
|
|
*/
|
|
|
- public function embedsCats($field,$lable)
|
|
|
|
|
|
|
+ public function embedsCats($field, $lable)
|
|
|
{
|
|
{
|
|
|
$modeClass = get_class($this->form->repository()->model());
|
|
$modeClass = get_class($this->form->repository()->model());
|
|
|
- $cates = $this->form->repository()->model()->getCasts();
|
|
|
|
|
|
|
+ $cates = $this->form->repository()->model()->getCasts();
|
|
|
// dump($cates);
|
|
// dump($cates);
|
|
|
- $class = $cates[$field] ?? "";
|
|
|
|
|
|
|
+ $class = $cates[$field] ?? "";
|
|
|
$reflectionClass = new \ReflectionClass($class);
|
|
$reflectionClass = new \ReflectionClass($class);
|
|
|
|
|
|
|
|
- return $this->form->embeds($field,$lable,function (\Dcat\Admin\Form\EmbeddedForm $form) use ($class,$reflectionClass){
|
|
|
|
|
|
|
+ return $this->form->embeds($field, $lable, function (\Dcat\Admin\Form\EmbeddedForm $form) use ($class, $reflectionClass) {
|
|
|
|
|
|
|
|
// 从注释中提取字段标签
|
|
// 从注释中提取字段标签
|
|
|
- $getFieldLabel = function($name, $comment) {
|
|
|
|
|
|
|
+ $getFieldLabel = function ($name, $comment) {
|
|
|
if ($comment) {
|
|
if ($comment) {
|
|
|
// 优先提取@label标签
|
|
// 优先提取@label标签
|
|
|
if (preg_match('/@label\s+([^\r\n]+)/', $comment, $matches)) {
|
|
if (preg_match('/@label\s+([^\r\n]+)/', $comment, $matches)) {
|
|
@@ -99,13 +102,14 @@ class FormHelper
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
// 默认使用字段名转换
|
|
// 默认使用字段名转换
|
|
|
- return ucwords(str_replace(['_', '-'], ' ', $name));
|
|
|
|
|
|
|
+ return ucwords(str_replace([ '_', '-' ], ' ', $name));
|
|
|
};
|
|
};
|
|
|
foreach ($reflectionClass->getProperties() as $property) {
|
|
foreach ($reflectionClass->getProperties() as $property) {
|
|
|
// 设置不同类型的表单
|
|
// 设置不同类型的表单
|
|
|
- $type = $property->getType() ? $property->getType()->getName() : null;
|
|
|
|
|
- $name = $property->getName();
|
|
|
|
|
|
|
+ $type = $property->getType() ? $property->getType()->getName() : null;
|
|
|
|
|
+ $name = $property->getName();
|
|
|
$comment = $property->getDocComment();
|
|
$comment = $property->getDocComment();
|
|
|
|
|
|
|
|
// 根据类型设置不同表单字段
|
|
// 根据类型设置不同表单字段
|
|
@@ -126,6 +130,7 @@ class FormHelper
|
|
|
$form->text($name, $getFieldLabel($name, $comment));
|
|
$form->text($name, $getFieldLabel($name, $comment));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
// 返回表单对象
|
|
// 返回表单对象
|
|
|
return $form;
|
|
return $form;
|
|
|
|
|
|
|
@@ -140,7 +145,7 @@ class FormHelper
|
|
|
* @param $default
|
|
* @param $default
|
|
|
* @return Form\Field\SwitchField
|
|
* @return Form\Field\SwitchField
|
|
|
*/
|
|
*/
|
|
|
- public function switch($field , $label = null)
|
|
|
|
|
|
|
+ public function switch($field, $label = null)
|
|
|
{
|
|
{
|
|
|
return $this->form->switch($field, $label);
|
|
return $this->form->switch($field, $label);
|
|
|
}
|
|
}
|
|
@@ -151,7 +156,7 @@ class FormHelper
|
|
|
* @param $field
|
|
* @param $field
|
|
|
* @return Form\Field\Number
|
|
* @return Form\Field\Number
|
|
|
*/
|
|
*/
|
|
|
- public function number($field, $label = null)
|
|
|
|
|
|
|
+ public function number($field, $label = null)
|
|
|
{
|
|
{
|
|
|
return $this->form->number($field, $label);
|
|
return $this->form->number($field, $label);
|
|
|
}
|
|
}
|
|
@@ -162,12 +167,12 @@ class FormHelper
|
|
|
* @param $field
|
|
* @param $field
|
|
|
* @return Form\Field\Text
|
|
* @return Form\Field\Text
|
|
|
*/
|
|
*/
|
|
|
- public function text($field, $label = null)
|
|
|
|
|
|
|
+ public function text($field, $label = null)
|
|
|
{
|
|
{
|
|
|
return $this->form->text($field, $label);
|
|
return $this->form->text($field, $label);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function hidden($field, $label = null)
|
|
|
|
|
|
|
+ public function hidden($field, $label = null)
|
|
|
{
|
|
{
|
|
|
return $this->form->hidden($field, $label);
|
|
return $this->form->hidden($field, $label);
|
|
|
}
|
|
}
|
|
@@ -178,7 +183,7 @@ class FormHelper
|
|
|
* @param $field
|
|
* @param $field
|
|
|
* @return Form\Field\Select
|
|
* @return Form\Field\Select
|
|
|
*/
|
|
*/
|
|
|
- public function select($field, $label = null)
|
|
|
|
|
|
|
+ public function select($field, $label = null)
|
|
|
{
|
|
{
|
|
|
return $this->form->select($field, $label);
|
|
return $this->form->select($field, $label);
|
|
|
}
|
|
}
|
|
@@ -190,23 +195,23 @@ class FormHelper
|
|
|
* @param array $enmu 枚举
|
|
* @param array $enmu 枚举
|
|
|
* @return Form\Field\Select|mixed
|
|
* @return Form\Field\Select|mixed
|
|
|
*/
|
|
*/
|
|
|
- public function selectOption($field, $enmu, $label = null)
|
|
|
|
|
|
|
+ public function selectOption($field, $enmu, $label = null)
|
|
|
{
|
|
{
|
|
|
return $this->form->select($field, $label)->options($this->useing($field, $enmu));
|
|
return $this->form->select($field, $label)->options($this->useing($field, $enmu));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function selectOptionCast($field, $label = null)
|
|
|
|
|
|
|
+ public function selectOptionCast($field, $label = null)
|
|
|
{
|
|
{
|
|
|
$modeClass = get_class($this->form->repository()->model());
|
|
$modeClass = get_class($this->form->repository()->model());
|
|
|
- $cates = $this->form->repository()->model()->getCasts();
|
|
|
|
|
|
|
+ $cates = $this->form->repository()->model()->getCasts();
|
|
|
// dump($cates);
|
|
// dump($cates);
|
|
|
- $enmu = $cates[$field] ?? "";
|
|
|
|
|
|
|
+ $enmu = $cates[$field] ?? "";
|
|
|
if ($enmu === '') {
|
|
if ($enmu === '') {
|
|
|
throw new \Exception("$field is not a model $modeClass casts");
|
|
throw new \Exception("$field is not a model $modeClass casts");
|
|
|
}
|
|
}
|
|
|
- $values = $enmu::getValueDescription();
|
|
|
|
|
|
|
+ $values = $enmu::getValueDescription();
|
|
|
|
|
|
|
|
- return $this->form->select($field, $label)->options( $values);
|
|
|
|
|
|
|
+ return $this->form->select($field, $label)->options($values);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -215,7 +220,7 @@ class FormHelper
|
|
|
* @param $field
|
|
* @param $field
|
|
|
* @return Form\Field\MultipleSelect
|
|
* @return Form\Field\MultipleSelect
|
|
|
*/
|
|
*/
|
|
|
- public function multipleSelect($field, $label = null)
|
|
|
|
|
|
|
+ public function multipleSelect($field, $label = null)
|
|
|
{
|
|
{
|
|
|
return $this->form->multipleSelect($field, $label);
|
|
return $this->form->multipleSelect($field, $label);
|
|
|
}
|
|
}
|
|
@@ -226,7 +231,7 @@ class FormHelper
|
|
|
* @param $field
|
|
* @param $field
|
|
|
* @return Form\Field\MultipleSelectTable
|
|
* @return Form\Field\MultipleSelectTable
|
|
|
*/
|
|
*/
|
|
|
- public function multipleSelectTable($field, $label = null)
|
|
|
|
|
|
|
+ public function multipleSelectTable($field, $label = null)
|
|
|
{
|
|
{
|
|
|
return $this->form->multipleSelectTable($field, $label);
|
|
return $this->form->multipleSelectTable($field, $label);
|
|
|
}
|
|
}
|
|
@@ -238,7 +243,7 @@ class FormHelper
|
|
|
* @param $field
|
|
* @param $field
|
|
|
* @return Form\Field\SelectTable
|
|
* @return Form\Field\SelectTable
|
|
|
*/
|
|
*/
|
|
|
- public function selectTable($field,$label = null)
|
|
|
|
|
|
|
+ public function selectTable($field, $label = null)
|
|
|
{
|
|
{
|
|
|
return $this->form->selectTable($field, $label);
|
|
return $this->form->selectTable($field, $label);
|
|
|
}
|
|
}
|
|
@@ -250,7 +255,7 @@ class FormHelper
|
|
|
* @param $field
|
|
* @param $field
|
|
|
* @return Form\Field\Checkbox
|
|
* @return Form\Field\Checkbox
|
|
|
*/
|
|
*/
|
|
|
- public function checkbox($field, $label = null)
|
|
|
|
|
|
|
+ public function checkbox($field, $label = null)
|
|
|
{
|
|
{
|
|
|
return $this->form->checkbox($field, $label);
|
|
return $this->form->checkbox($field, $label);
|
|
|
}
|
|
}
|
|
@@ -262,10 +267,11 @@ class FormHelper
|
|
|
* @param array $enmu
|
|
* @param array $enmu
|
|
|
* @return Form\Field\Radio
|
|
* @return Form\Field\Radio
|
|
|
*/
|
|
*/
|
|
|
- public function radio($field, array $enmu, $label = null )
|
|
|
|
|
|
|
+ public function radio($field, array $enmu, $label = null)
|
|
|
{
|
|
{
|
|
|
$lable = $lable ?? $field;
|
|
$lable = $lable ?? $field;
|
|
|
- return $this->form->radio($field, $label)->options( $enmu);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ return $this->form->radio($field, $label)->options($enmu);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -276,7 +282,7 @@ class FormHelper
|
|
|
*
|
|
*
|
|
|
* @return Form\Field\ListField
|
|
* @return Form\Field\ListField
|
|
|
*/
|
|
*/
|
|
|
- public function list($field, $label = null)
|
|
|
|
|
|
|
+ public function list($field, $label = null)
|
|
|
{
|
|
{
|
|
|
return $this->form->list($field, $label);
|
|
return $this->form->list($field, $label);
|
|
|
}
|
|
}
|
|
@@ -299,14 +305,14 @@ class FormHelper
|
|
|
* @param $field
|
|
* @param $field
|
|
|
* @return $this
|
|
* @return $this
|
|
|
*/
|
|
*/
|
|
|
- public function editor($field, $label = null)
|
|
|
|
|
|
|
+ public function editor($field, $label = null)
|
|
|
{
|
|
{
|
|
|
$this->form->editor($field, $label);
|
|
$this->form->editor($field, $label);
|
|
|
|
|
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function multipleImage($field, $label = null)
|
|
|
|
|
|
|
+ public function multipleImage($field, $label = null)
|
|
|
{
|
|
{
|
|
|
$this->form->multipleImage($field, $label);
|
|
$this->form->multipleImage($field, $label);
|
|
|
|
|
|
|
@@ -333,7 +339,7 @@ class FormHelper
|
|
|
* @param $field
|
|
* @param $field
|
|
|
* @return Form\Field\Display
|
|
* @return Form\Field\Display
|
|
|
*/
|
|
*/
|
|
|
- public function display($field, $label = null)
|
|
|
|
|
|
|
+ public function display($field, $label = null)
|
|
|
{
|
|
{
|
|
|
return $this->form->display($field, $label);
|
|
return $this->form->display($field, $label);
|
|
|
}
|
|
}
|
|
@@ -342,11 +348,12 @@ class FormHelper
|
|
|
* @param $field
|
|
* @param $field
|
|
|
* @return Form\Field\Display
|
|
* @return Form\Field\Display
|
|
|
*/
|
|
*/
|
|
|
- public function displayOptions($field, $label = null)
|
|
|
|
|
|
|
+ public function displayOptions($field, $label = null)
|
|
|
{
|
|
{
|
|
|
- $c = $this->controller;
|
|
|
|
|
- return $this->form->display($field, $label)->with(function($value) use ($field,$c) {
|
|
|
|
|
- return $c->_option($field.'-'.$value);
|
|
|
|
|
|
|
+ $c = $this->controller;
|
|
|
|
|
+
|
|
|
|
|
+ return $this->form->display($field, $label)->with(function ($value) use ($field, $c) {
|
|
|
|
|
+ return $c->_option($field . '-' . $value);
|
|
|
|
|
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
@@ -358,20 +365,21 @@ class FormHelper
|
|
|
* @return $this
|
|
* @return $this
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
- public function textarea($field, $label = null)
|
|
|
|
|
|
|
+ public function textarea($field, $label = null)
|
|
|
{
|
|
{
|
|
|
return $this->form->textarea($field, $label);
|
|
return $this->form->textarea($field, $label);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 增加表格
|
|
* 增加表格
|
|
|
|
|
+ *
|
|
|
* @param $field
|
|
* @param $field
|
|
|
* @param $call
|
|
* @param $call
|
|
|
* @return Form\Field\Table
|
|
* @return Form\Field\Table
|
|
|
*/
|
|
*/
|
|
|
- public function table($field,$call, $label = null)
|
|
|
|
|
|
|
+ public function table($field, $call, $label = null)
|
|
|
{
|
|
{
|
|
|
- return $this->form->table($field, $label,$call);
|
|
|
|
|
|
|
+ return $this->form->table($field, $label, $call);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -381,13 +389,14 @@ class FormHelper
|
|
|
* @param $field
|
|
* @param $field
|
|
|
* @return Form\Field\Tags
|
|
* @return Form\Field\Tags
|
|
|
*/
|
|
*/
|
|
|
- public function tags($field, $label = null)
|
|
|
|
|
|
|
+ public function tags($field, $label = null)
|
|
|
{
|
|
{
|
|
|
return $this->form->tags($field, $label);
|
|
return $this->form->tags($field, $label);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 关闭所有的 多余
|
|
* 关闭所有的 多余
|
|
|
|
|
+ *
|
|
|
* @return void
|
|
* @return void
|
|
|
*/
|
|
*/
|
|
|
public function disableAll()
|
|
public function disableAll()
|
|
@@ -409,4 +418,22 @@ class FormHelper
|
|
|
$this->form->disableEditingCheck();
|
|
$this->form->disableEditingCheck();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * select模型选项
|
|
|
|
|
+ * @param $field
|
|
|
|
|
+ * @param $label
|
|
|
|
|
+ * @param $modelClass
|
|
|
|
|
+ * @param $name
|
|
|
|
|
+ * @param $id
|
|
|
|
|
+ * @return Form\Field\Select|mixed
|
|
|
|
|
+ */
|
|
|
|
|
+ public function selectModelOption($field, $label, $modelClass,$name = 'name', $id = 'id')
|
|
|
|
|
+ {
|
|
|
|
|
+ $options = $modelClass::all()->pluck($name, $id);
|
|
|
|
|
+
|
|
|
|
|
+ return $this->form->select($field, $label)->options($options);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|