承接 wjd/wjd-json-form-builder 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

wjd/wjd-json-form-builder

Composer 安装命令:

composer require wjd/wjd-json-form-builder

包简介

Loads external JSON to render forms

README 文档

README

Introduction

The wjd-json-form-builder is a PHP library designed to generate HTML forms based on JSON schemas. It provides functionality to create form markup dynamically by either fetching JSON data from an API endpoint or by processing a provided JSON string.

Latest Stable Version License

Installation

You can install the library via Composer:

composer require wjd/wjd-json-form-builder

Usage

Functions

Both functions return markup based on the provided JSON schema. The getFromApi function uses the getFromSchema function internally to process the data. Endpoints currently must always be publicly accessible.

getFromApi

getFromApi(string $getApiEndpoint)

Fetches a URL that returns a valid JSON format. The JSON structure is explained below.

getFromSchema

getFromSchema(string $json)

Accepts a JSON string and generates the corresponding form markup.

JSON Schema

{
    "$id": "http://mac-gyver.de/json/form.json",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "Form",
    "type": "object",
    "properties": {
        "firstName": {
            "type": "string",
            "minLength": 1,
            "description": "First Name",
            "default": "Max"
        },
        "email": {
            "type": "string",
            "minLength": 1,
            "format": "email",
            "description": "Email"
        },
        "website": {
            "type": "string",
            "minLength": 1,
            "format": "uri",
            "description": "Website"
        },
        "birthdate": {
            "type": "string",
            "minLength": 1,
            "format": "date",
            "description": "Birthdate"
        },
        "age": {
            "type": "integer",
            "description": "Age",
            "minimum": 18,
            "maximum": 120
        },
        "gdpr": {
            "type": "boolean",
            "description": "Agreed to GDPR"
        },
        "selection": {
            "type": "object",
            "description": "Selection",
            "default": "2",
            "properties": {
                "1": {
                    "type": "string",
                    "description": "Option 1"
                },
                "2": {
                    "type": "string",
                    "description": "Option 2"
                },
                "3": {
                    "type": "string",
                    "description": "Option 3"
                }
            }
        },
        "password": {
            "type": "string",
            "description": "Password",
            "pattern": "(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,}",
            "additionalProperties": {
                "hint": {
                    "type": "string",
                    "description": "The password must contain at least one uppercase letter, one lowercase letter, one number, and be at least 8 characters long."
                },
                "confirm": {
                    "type": "boolean",
                    "default": true
                },
                "show": {
                    "type": "boolean",
                    "default": true
                }
            }
        },
        "honeypot": {
            "type": "string",
            "description": "obvious_honeypot"
        },
        "hidden": {
            "type": "string",
            "default": "2"
        }
    },
    "additionalProperties": {
        "action": {
            "type": "string",
            "default": "http://wjdjson.localdev/receiver.php"
        }
    },
    "required": [
        "email",
        "age",
        "gdpr"
    ]
}

Example Implementation

Below is an example of how to use the wjd-json-form-builder in your project:

<?php

namespace WjdDesignStudio\Helpers;

defined('ABSPATH') or die();

use WjdJsonFormBuilder\Form\Creator;

class CreateGeneratorForm {
    public function createFromSchema($endpoint, $schema) {
        $creator = new Creator();
        $form = $creator->getFromApi($schema, [
            'action' => '/',
            'method' => 'POST',
            'submit' => 'Generate PDF'
        ]);

        ob_start(); ?>
        <div id="loader-<?= $form['id'] ?>" style="display:none;">
            <img src="<?= WJD_DESIGN_STUDIO_PLUGIN_URL ?>/img/generate.svg" />
        </div>
        <a id="generation-<?= $form['id'] ?>" style="display:none;"></a>
        <script>
            window.addEventListener('wjdJsonFormBuilderScriptsLoaded', () => {
                const form = document.querySelector('.form-' + '<?= $form['id'] ?>');
                const loader = document.getElementById("loader-<?= $form['id'] ?>");
                const generation = document.getElementById("generation-<?= $form['id'] ?>");

                generation.style.display = "none";
                loader.style.display = "none";

                form.addEventListener('submit', (e) => {
                    e.preventDefault();
                    let data = wjdJsonFormToJson('<?= $form['id'] ?>');
                    console.log(data);

                    generation.style.display = "none";
                    loader.style.display = "block";

                    fetch('<?= $endpoint ?>', {
                        headers: {
                            "Content-Type": "application/json"
                        },
                        body: JSON.stringify(data),
                        method: "POST"
                    })
                    .then(response => response.json())
                    .then(json => {
                        loader.style.display = "none";
                        generation.style.display = "block";
                        console.log(json);
                        generation.innerHTML = `The print file has been generated and can be downloaded <a href="${json.result}" target="_blank">here</a>.`;
                    })
                    .catch(error => console.log(error))
                });
            })
        </script>
        <?php 
        $extension = ob_get_clean();
        return $form['markup'].$extension;
    }
}

Contribute and Maintain

  • Testing: Always test your changes before pushing to the repository. If you are uncertain about your changes, create a feature branch.
  • Changelog: Update the CHANGELOG.md file following Semantic Versioning.
  • Tags: Create a tag for each published version to be published to Packagist for Composer usage.

wjd/wjd-json-form-builder 适用场景与选型建议

wjd/wjd-json-form-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 10 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 wjd/wjd-json-form-builder 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-26