magic-form

laravel-admin magic-form

0 0 0 0
  • Created: Jun 11, 2019
  • Last Release: Jun 16, 2019
  • Last Commit: Jun 16, 2019
by ichynul

laravel-admin magic-form

About

Keep code Style same for Form/Grid/Show

使 Form/Grid/Show 的代码风格统一.


$grid->name('昵称');

//$form->text('name','昵称');

$form->name('昵称')->text(); // Just change `$grid` to `$form` then append `->text()` at behind;

$show->name('昵称');

Installation

Run :

$ composer require ichynul/magic-form

Config

Add a config in config/admin.php:

    'extensions' => [
        'magic-form' => [
           //Equals Ichynul\MagicForm::useMagic(false); when web boot up
            'enable' => false,
            //kes recognise as elements ,not columns 
            'specials' => ['html', 'divider', 'divide']
        ]
    ],

Usage


<?php

namespace App\Admin\Controllers;

//use Encore\Admin\Form;
use Ichynul\MagicForm\Form;
use App\Models\User;
// use ...

class UsersController extends Controller
{
    use HasResourceActions;

    /******/

    /**
     * Edit interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->breadcrumb(
                ['text' => '用户管理', 'url' => 'users'],
                ['text' => '编辑', 'url' => '/']
            )
            ->body($this->form()->edit($id));
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        $form::useMagic(true); // if config of 'extensions.magic-form.enable' seted to 'false' , enabel it here,
        // You can use new code style

        $form->tab('基本信息', function ($form) {

            $form->id('编号')->display();

            $form->name('昵称')->text()->rules('required');

            $form->avatar('头像')->cropper()->cRatio(100, 100)->rules('required');

        });

        $form->tab('扩展信息', function ($form) {
            $form->gender('性别')->radio([0 => '未知', 1 => '男', 2 => '女']);
            // checkbox radio select listbox multipleSelect
            // Equals $form->gender('性别')->radio()->options([0 => '未知', 1 => '男', 2 => '女']);

            $form->_()->map('latitude', 'longitude', '坐标');
            $form->_()->textarea('divider','Column-divider');
            $form->_()->text('html','Column-html');
            // $form->_()->element()... change to old code style temporarily 临时切换到旧写法
            // For some elements we need to change to old code style to use them
            // The map need 3 agrs , The [divider/divide/html] recognise as elements by default

            $form->divider();

            $form->mobile('手机号')->text();

            $form->html('123456');

        });

        $form::useMagic(false);// You can use old code style bellow

        $form->tab('修改密码', function ($form) {

            $form->password('password', '新密码')->default(function ($form) {
                return $form->model()->password;
            });

            $form->select('blocked', '锁定')->options([0 => '正常', 1 => '已锁定'])->help('锁定状态的用户将无法登录');

            $form->display('created_at', '注册时间');

            $form->display('updated_at', trans('admin.updated_at'));
        });

        return $form;
    }

Known issuse

function (Form\Tools $tools) will make some errors.


$form->tools(function (Form\Tools $tools) { //wrong
    //..
});

$form->tools(function ($tools) { //right
    //..
});

$form->tools(function (\Encore\Admin\Form\Tools $tools) { //right
   //..
});