定制 corollarium/formularium 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

corollarium/formularium

Composer 安装命令:

composer require corollarium/formularium

包简介

Form validation and generation for PHP with custom frontend generators

关键字:

README 文档

README

Build Status Code Coverage Latest Stable Version Total Downloads License Scrutinizer Code Quality

Formularium (full documentation) is a typed code generator for PHP from a uniform model description. Are you tired of updating code in multiple places whenever you change a model? Making typos and bugs, or forgetting to change that 8th file? Formularium will:

  • generate HTML/JS/TS code, such as forms, views and elements with multiple target JS and CSS frameworks
  • generate backend code for model descriptions (such as SQL tables, Typescript interfaces, GraphQL types)
  • validate data in PHP

Formularium already implements a number of generators for different languages and CSS/JS frameworks, as well as validators for backend and frontend, abstracting code for you. It's easily extensible to target your favorite language or framework as well.

If you are looking for a fully integrated backend/frontend scaffolding and validation, Modelarium is what you want, with bindings for Laravel. Formularium is the low-level generator used by Modelarium.

Philosophy

Formularium is based on two principles.

Your data are not just strings. Type your fields.

Formularium implements high level data types, allowing you to specify exactly what you expect of each field. With well defined types you can easily generate code for datatype creation, validation and form generation. A Title is not a pure string: it's a datatype with minimum and maximum lengths, instructions of how it should be rendered etc.

It's also easy to create new datatypes, either from scratch or extending the base types provided.

Code generation won't get 100% of cases, but it should make your life easy 100% of the time.

If entire applications could be easily generated by code, we'd have done it by now. The reason they can't is because there are all kinds of details that cannot be easily compressed into a simple expression. Formularium is essentially a code compressor: it generates code from a reduced model description.

For any lossless compressor, some descriptions are longer when compressed than the original. Formularium doesn't strive to handle all the infinite possible cases. It generates code for you that's simple and easy to modify or extend, and doesn't get in your way. Then you can extend, override or even manually change the generated code to suit your needs.

In the best scenario you get all your code written automatically for you. In the worst case scenario you just override the code that doesn't suit you. But it doesn't ever get in your way: it generates pure code at development time..

Frontend generation examples

Examples of the same PHP model automatically generating frontend forms in Bootstrap, Bulma, Materialize and Buefy. Click on the images to see live HTML.

These forms are all generated from the same simple data structure, which describes its fields with a datatype and general information for the HTML generator (such as labels). Model descriptions can be serialized as JSON.

If you are looking for a fully integrated backend/frontend scaffolding and validation, Modelarium is what you want. Formularium is the low-level generator used by Modelarium.

Getting started

Check the:

Why you should use Formularium

  • typed data system: change the datatype and automatically reflect it on your data. So your field grew from 30 characters to 50? Change the datatype to reflect it and get everything updated at once.
  • automatic validation: validate your data automatically and safely.
  • component validators: create new datatypes and add new validators easily.
  • HTML abstraction: abstract your CSS frameworks for simple element generation from code, like buttons or tables.
  • GraphQL description: declare your models in GraphQL SDL and get the scaffolding for free. You don't even need to write PHP code for Formularium. Works even better with Modelarium.
  • quickly generate frontend scaffolding: stop writing verbose HTML and have manual labor to generate forms/cards/etc. Let this tool do all the basic work for you and just refine the design if necessary.
  • component generation: generate React and Vue components with the corresponding HTML template.
  • automatic backend code generator: SQL/Graphql/Typescript/Laravel models. Convert your models to your database descriptions with SQL and Laravel types, or quickly generate Graphql and JS code.

Sponsors

Corollarium

Minimum example

Everything in a glance:

// set your framework composition statically.
// For example, this builds HTML using Bootstrap as CSS and the Vue framework.
$composer = new FrameworkComposer(['HTML', 'Bootstrap', 'Vue']);

// first, you can just generate simple standalone elements, like a button:
echo $composer->element(
    'Button',
    [
        Element::LABEL => 'Submit',
        Element::SIZE => Element::SIZE_LARGE,
    ]
);

// build a model from data description. You can use a JSON file instead, or a GraphQL file.
$modelData = new Model(
    'TestModel',
);
$modelData->appendField(new Field(
    'myString',
    Datatype_string::class,
    [
        Renderable::LABEL => 'This is some string',
        Renderable::COMMENT => 'At least 3 characters but no more than 10',
        Renderable_string::NO_AUTOCOMPLETE => true,
        Renderable::PLACEHOLDER => "Type here",
        Renderable::ICON_PACK => 'fas',
        Renderable::ICON => 'fa-check'
    ],
    [
        MinLength::class => [
            'value' => 3,
        ],
        MaxLength::class => [
            'value' => 10,
        ],
        Datatype::REQUIRED => [
            'value' => true,
        ]
    ],
))->appendField(new Field(
    'someInteger',
    'integer',
    [
        Renderable_number::STEP => 2,
        Renderable_string::NO_AUTOCOMPLETE => true,
        Renderable::LABEL => 'Some integer',
        Renderable::COMMENT => 'Between 4 and 30',
        Renderable::PLACEHOLDER => "Type here"
    ],
    [
        Min::class => [
            'value' => 4,
        ],
        Max::class => [
            'value' => 30,
        ],
        Datatype::REQUIRED => [
            'value' => true,
        ]
    ],
))->appendField(new Field(
    'myaaaaa',
    'aaaaa',
    [
        Renderable::LABEL => 'This is a custom datatype',
        Renderable::COMMENT => 'Fill this with aaaaa to validate properly',
        Renderable_string::NO_AUTOCOMPLETE => true,
        Renderable::PLACEHOLDER => "Type aaaaa here"
    ],
    [
        Datatype::REQUIRED => [
            'value' => true,
        ]
    ],
));

// validate some data
$data = [
    'myString' => 'some string here',
    'someInteger' => 32
];
$validation = $model->validate($data);
if (!empty($validation['errors'])) {
    foreach ($validation['errors'] as $fieldName => $error) {
        echo "$fieldName has an error: $error\n";
    }
}
// get data after validation
$validated = $validation['validated'];

// render a form
echo $model->editable($data);

// render a view
echo $model->viewable($data);

// generate a typescript interface, like `type TestModel { ... }`
$codeGenerator = new \Formularium\CodeGenerator\Typescript\CodeGenerator();
echo  $codeGenerator->type($model);

The output is a nice HTML that you can use as basis for your forms. See the generated HTML on the kitchen sink examples.

Supported frontend generators

Formularium is built in a way that generators can be chained, so you can combine a basic HTML form generator, with a CSS framework and a JS validator, or possibly get the form into a Vue or React component. We provide a number of frontend plugins and you can easily extend with your own (and submit a PR!).

Contributing contributions welcome

Any contributions are welcome. Please send a PR.

We are looking for people experienced in React and Angular to work on its generators.

corollarium/formularium 适用场景与选型建议

corollarium/formularium 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 980 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「form」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 980
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 9
  • 依赖项目数: 1
  • 推荐数: 1

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-09