sujan/laraform 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sujan/laraform

Composer 安装命令:

composer require sujan/laraform

包简介

HTML and Form Builders for the Laravel Framework from YAML and JSON

README 文档

README

Developing rapid forms from YAML and JSON, when large number of input fields will be required as well as frequent addition and deduction of form fields.

Installation

composer require "sujan/laraform"

As sujan/laraform package is based on laravelcollective/html you have to add your new provider to the providers array of config/app.php:

'providers' => [
    // ...
    Sujan\LaraForm\ServiceProvider::class,
    Collective\Html\HtmlServiceProvider::class,
    // ...
],

Finally, add two class aliases to the aliases array of config/app.php:

'aliases' => [
    // ...
    'Form' => Collective\Html\FormFacade::class,
    'Html' => Collective\Html\HtmlFacade::class,
    // ...
],

Then run the command

php artisan vendor:publish

It will give you a laraform.css file in /css/laraform.css

Then add laraform.css to your master template like

<link rel="stylesheet" href="{{ asset('css/laraform.css') }}">

Usage

This package has multi purpose usage. You will get all the facilities of Laravel Collective as well as facilities of sujan/laraform package. We developed this package based on real life scenario.

Scenario 1

Say you have to develop a key value store where all of your application settings will reside. The kick here is to add more and more new keys when needed but no code modification will be needed. Then this is the right package for you. All you have to do is to write a YAML or JSON file for your form.

Scenario 2

Let's assume the values of your settings will come as a nested object and you have to make a form for the object then this package is a good choice. It will build the form for you within a few minutes all you have to do is write the JSON or YAML file based on the JSON object.

How to Use

Use it inside form tag in your form like

{{ Form::yaml("path/to/file.yml") }}
{{ Form::json("path/to/file.json") }}

Laraform support input types text, email, password, number, hidden, date, file, textarea, checkbox, select, radio, checkboxlist, section;

Example 1

Let's say you have a users table and a usersmeta table. In users table you will save name,email, password and in usersmeta table you want to save date_of_birth, color, gender, address, favourites, profile_pic. Your yaml and json file will be like below. Where usermeta is relation name. Our package will parse the form for you.

Sample YAML for Example 1

fields:
    name:
      label: Name
      type: text
      span: left
      
    email:
      label: Email
      type: email
      span: right
      
    password:
      label: Password
      type: password
      span: left
      
    password_confirmation:
      label: Confirm Password
      type: password
      span: right
      
    metadata:
      label: User Meta
      type: section
      
    usermeta[date_of_birth]:
      label: Date of Birth
      type: date
      span: left
      
    usermeta[color]:
      label: Choose Color
      type: select
      span: right
      options:
        "": Choose Color
        red: Red
        green: Green
        blue: Blue
        
    usermeta[gender]:
      label: Gender
      type: radio
      span: left
      options:
        male: Male
        female: Female
        other: Other
        
    usermeta[favourites][]:
      label: Favourites
      type: checkboxlist
      span: right
      options:
        cake: Cake
        apple: Apple
        mango: Mango
        
    usermeta[address]:
      label: Address
      type: textarea
      
    usermeta[profile_pic]:
      label: Upload profile picture
      type: file

Sample JSON for Example 1

{
  "fields": {
    "name": {
      "label": "Name",
      "type": "text",
      "span": "left"
    },
    "email": {
      "label": "Email",
      "type": "email",
      "span": "right"
    },
    "password": {
      "label": "Password",
      "type": "password",
      "span": "left"
    },
    "password_confirmation": {
      "label": "Confirm Password",
      "type": "password",
      "span": "right"
    },
    "metadata": {
      "label": "User Meta",
      "type": "section"
    },
    "usermeta[date_of_birth]": {
        "label": "Date of Birth",
        "type": "date",
        "span": "left"
    },
    "usermeta[color]": {
      "label": "Choose Color",
      "type": "select",
      "span": "right",
      "options": {
        "": "Choose Color",
        "red": "Red",
        "green": "Green",
        "blue": "Blue"
      }
    },
    "usermeta[gender]": {
      "label": "Gender",
      "type": "radio",
      "span": "left",
      "options": {
        "male": "Male",
        "female": "Female",
        "other": "Other"
      }
    },
    "usermeta[favourites][]": {
      "label": "Favourites",
      "type": "checkboxlist",
      "span": "right",
      "options": {
        "cake": "Cake",
        "apple": "Apple",
        "mango": "Mango"
      }
    },
    "usermeta[address]": {
      "label": "Address",
      "type": "textarea"
    },
    "usermeta[profile_pic]": {
      "label": "Upload profile picture",
      "type": "file"
    }
  }
}

Here span left, right is used to push the input fields to left and right. If you don’t specify span then the span will be full by default.

Sample create form

{{ Form::open(['method' => 'POST']) }}
{{ Form::yaml(/path/to/yaml/file) }}
{{ Form::submit('Submit', ['class' => 'form-control']) }}
{{ Form::close() }}

Html form of sample YAML and JSON.

Sample form

Form validation rule:

$this->validate($request, [
    'name' => 'required',
    'email' => 'required',
    'password' => 'required|confirmed',
    'usermeta.gender' => 'required',
    'usermeta.favourites' => 'required',
    'usermeta.address' => 'required',
    'usermeta.date_of_birth' => 'required',
    'usermeta.color' => 'required',
    'usermeta.profile_pic' => 'required|mimes:jpg,png',
],[
    'usermeta.address.required' => 'The address field is required.',
    'usermeta.favourites.required' => 'The favourites field is required.',
    'usermeta.date_of_birth.required' => 'The date of birth field is required.',
    'usermeta.color.required' => 'The color field is required.',
    'usermeta.gender.required' => 'The gender field is required.',
    'usermeta.profile_pic.required' => 'The profile picture field is required.',
]);

Html form of sample YAML and JSON with validation message.

Sample form with validation

The YAML and JSON files work for both create and edit page, all you have to do is to pass the model instance or JSON object.

Sample edit form for the above example.

{{ Form::model($model, ['method' => 'POST']) }}
{{ Form::yaml(/path/to/yaml/file) }}
{{ Form::submit('Submit', ['class' => 'form-control']) }}
{{ Form::close() }}

Here variable $model is model instance or JSON object like below.

Sample JSON object

{
    "name": "John Doe",
    "email": "john@example.com",
    "usermeta": {
        "date_of_birth": "2018-04-19",
        "color": "green",
        "gender": "male",
        "favourites": [
            "cake",
            "mango"
        ],
        "address": "No where"
    }
}

Sample edit form of the above JSON object

Sample edit form

Sample edit form with validation message

Sample edit form with validation

The yaml and json file for the above object will be #Sample 1's two files.

Form validation

You have to use laravel's form validation methodology. The error message handling is included in the package. So you don't have to write code for showing error messages. The message will be shown below the input field marked red.

sujan/laraform 适用场景与选型建议

sujan/laraform 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 215 次下载、GitHub Stars 达 46, 最近一次更新时间为 2018 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 sujan/laraform 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sujan/laraform 我们能提供哪些服务?
定制开发 / 二次开发

基于 sujan/laraform 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 215
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 46
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 46
  • Watchers: 2
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-04-04