https://github.com/mathsgod/formkit-php
A PHP library for generating FormKit Schema from PHP classes.
https://github.com/mathsgod/formkit-php
formkit php
Last synced: 8 months ago
JSON representation
A PHP library for generating FormKit Schema from PHP classes.
- Host: GitHub
- URL: https://github.com/mathsgod/formkit-php
- Owner: mathsgod
- License: mit
- Created: 2023-08-17T04:10:27.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-08-18T09:05:24.000Z (over 2 years ago)
- Last Synced: 2025-03-13T09:03:24.922Z (10 months ago)
- Topics: formkit, php
- Language: PHP
- Homepage:
- Size: 41 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# formkit-php
A PHP library for generating FormKit Schema from PHP classes.
## Installation
Install via composer:
```bash
composer require mathsgod/formkit-php
```
## Usage
### Basic Usage
```php
$schema = new FormKit\Schema();
$schema->appendHTML("");
echo json_encode($schema, JSON_PRETTY_PRINT);
```
output:
```json
[
{
"$formkit": "text",
"label": "hello"
}
]
```
### Simple element
```php
$schema = new FormKit\Schema();
$schema->appendElement("div")->appendElement("span")->appendHTML("hello");
echo json_encode($schema, JSON_PRETTY_PRINT);
```
output:
```json
[
{
"$el": "div",
"children": [
{
"$el": "span",
"children": [
"hello"
]
}
]
}
]
```
### Component
```php
$schema = new FormKit\Schema();
$card=$schema->appendComponent("q-card");
$card->setAttribute("flat","");
echo json_encode($schema, JSON_PRETTY_PRINT);
```
output:
```json
[
{
"$cmp": "q-card",
"props": {
"flat": true
}
}
]
```
### Registering custom Vue components
```php
$schema = new FormKit\Schema();
$schema->registerClass("q-card", FormKit\Component::class);
$schema->appendHTML("Hello");
echo json_encode($schema, JSON_PRETTY_PRINT);
```
output:
```json
[
{
"$cmp": "q-card",
"props": {
"flat": true
},
"children": [
"Hello"
]
}
]
```
### Registering custom FormKit input components
```php
$schema = new FormKit\Schema();
$schema->registerInputClass("my-input", FormKit\FormKitInputs::class);
$schema->appendHTML("");
echo json_encode($schema, JSON_PRETTY_PRINT);
```
output:
```json
[
{
"$formkit": "my-input",
"label": "My custom input"
}
]
```
### Custom component class
```php
class QBtn extends FormKit\Component
{
public function setLabel($label)
{
$this->setAttribute("label", $label);
}
}
$schema = new FormKit\Schema();
$schema->registerClass("q-btn", QBtn::class);
$node=$schema->appendHTML("")[0]; //$node=$schema->appendComponent("q-btn");
//change label
$node->setLabel("World");
echo json_encode($schema, JSON_PRETTY_PRINT);
```
output:
```json
[
{
"$cmp": "q-btn",
"props": {
"label": "World"
}
}
]
```
### Append child nodes
```php
$schema = new FormKit\Schema();
$e = $schema->appendHTML("
")[0];
$e->append($schema->createElement("div", "hello"));
echo json_encode($schema, JSON_PRETTY_PRINT);
```
output:
```json
[
{
"$el": "div",
"children": [
{
"$el": "div",
"children": [
"hello"
]
}
]
}
]
```
### Append child nodes from HTML
```php
$schema = new FormKit\Schema();
$e = $schema->appendHTML("
")[0];
$e->appendHTML("hello");
echo json_encode($schema, JSON_PRETTY_PRINT);
```
output:
```json
[
{
"$el": "div",
"children": [
{
"$el": "div",
"children": [
"hello"
]
}
]
}
]
```
### Loops
```php
$group = $schema->appendHTML("")[0];
$group->setAttribute(":value", json_encode([
"cities" => ["Hong Kong", "Taiwan", "China"]
]));
$div = $group->appendElement("div");
$div->for(["item", "key", '$value.cities']);
$div->appendHTML('$item');
echo json_encode($schema, JSON_PRETTY_PRINT);
```
output:
```json
[
{
"$formkit": "group",
"label": "Group",
"value": {
"cities": [
"Hong Kong",
"Taiwan",
"China"
]
},
"children": [
{
"$el": "div",
"children": [
"$item"
],
"for": [
"item",
"key",
"$value.cities"
]
}
]
}
]
```