An open API service indexing awesome lists of open source software.

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.

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"
]
}
]
}
]
```