表单生成类

{warning} FormHandle类用于快速创建一个新的表单类。命名空间:

  • ElementUI FormBuilder\Handle\ElmFormHandle
  • Iview FormBuilder\Handle\IviewFormHandle
  • 基础类 FormBuilder\FormHandle

注意: form-builder 版本需要大于 2.0.6

使用 FormHandle 创建一个表单

<?php
namespace App;

use FormBuilder\Handle\IviewFormHandle;
use FormBuilder\UI\Iview\Components\DatePicker;
use FormBuilder\UI\Iview\Components\Rate;
use FormBuilder\Factory\Iview;

class GoodsForm extends IviewFormHandle
{
    //表单提交地址
    protected $action = 'save.php';
    //页面标题
    protected $title = '测试 Handle';
    //组件的名称
    protected $fieldTitles = [
        'start_time' => '开启时间',
        'star'=>'点赞'
    ];
    //设置过滤的组件
    protected $except = [];
    //设置场景值
    protected $scene;

    //iview4.x 版本可设置为4
    protected $version;

    //创建一个 input 组件
    public function goods_name_field()
    {
        return Iview::input('goods_name', '商品名称')->required();
    }

    //创建一个 textarea 组件
    public function goods_info_field()
    {
        return Iview::textarea('goods_info', '商品简介');
    }

    //创建一个 switch 组件
    public function is_open_field()
    {
        return Iview::switches('is_open', '是否开启');
    }

    /**
     * 通过依赖注入方式生成组件
     *
     * @param DatePicker $date
     * @return DatePicker
     */
    public function start_time_field(DatePicker $date)
    {
        return $date->required()->info('asdfasdfasdfsf');
    }

    //创建一个 rate 组件
    public function starField(Rate $rate)
    {
        return $rate;
    }

    //设置表单配置
    protected function getFormConfig()
    {
        $config = Iview::config();
        $config->createResetBtn()->show(true);

        return $config;
    }

    //当场景值为 post 时触发此方法
    protected function postScene()
    {

    }

    //设置表单默认值
    protected function getFormData()
    {
        return [
            'goods_name' => 'goods_name123',
            'asdf' => 'asdfafd',
            'is_open' => '0',
            'goods_info' => "asdf\r\nadfa",
            'start_time' => '1999-11-11',
            'star'=>0
        ];
    }
}

创建一个组件

若要定义一个组件,则需在 FormHandle 上面定义 ComponentNameField 方法。让我们再来定义一个 goods_name 的组件。

<?php 
namespace App;

use FormBuilder\Handle\IviewFormHandle;
use FormBuilder\UI\Iview\Components\Input;

class GoodsForm extends IviewFormHandle
{
    //表单提交地址
    protected $action = 'save.php';

    public function goods_name_field(){
        return Iview::input('goods_name','商品名称');
    }
}

或者使用依赖注入的方式

<?php 
namespace App;

use FormBuilder\Handle\IviewFormHandle;
use FormBuilder\UI\Iview\Components\Input;
class GoodsForm extends IviewFormHandle
{
    //表单提交地址
    protected $action = 'save.php';

    protected $fieldTitles = [
        'goods_name'=>'商品名称'    
    ];

    public function goods_name_field(Input $input){
        return $input;
    }
}

使用「驼峰式」命名

<?php 
namespace App;

use FormBuilder\Handle\IviewFormHandle;
use FormBuilder\UI\Iview\Components\Input;
class GoodsForm extends IviewFormHandle
{
    //表单提交地址
    protected $action = 'save.php';

    protected $fieldTitles = [
        'goodsName'=>'商品名称'    
    ];

    public function goodsNameField(Input $input){
        return $input;
    }
}

获取表单类

<?php
$formHandle = new GoodsForm();
$form = $formHandle->form();

获取表单页面

<?php
$formHandle = new GoodsForm();
$html = $formHandle->view();
Powered by