Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobg committed Dec 12, 2019
1 parent dc22a99 commit 8df9f3b
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 1 deletion.
28 changes: 28 additions & 0 deletions Datatype_aaaaa.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Formularium\Datatype;

use Formularium\Field;
use Formularium\Exception\ValidatorException;

class Datatype_aaaaa extends \Formularium\Datatype\Datatype_string
{
public function __construct($typename = 'aaaaa', $basetype = 'string')
{
parent::__construct($typename, $basetype);
}

public function getRandom(array $params = [])
{
return 'aaaaa';
}

public function validate($value, Field $field)
{
$value = parent::validate($value, $field);
if ($value === 'aaaaa') {
return $value;
}
throw new ValidatorException('Not aaaaa');
}
}
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# Formularium-example

A simple pure PHP app showing how to use [Formularium](https://github.com/Corollarium/Formularium)
A simple pure PHP app showing how to use [Formularium](https://github.com/Corollarium/Formularium).

## About

This is a minimum simple example of how to use Formularium, using plain PHP without any PHP frameworks. It uses [Bootstrap](https://getbootstrap.com/) for styling.

- [index.php](https://github.com/Corollarium/Formularium-example/blob/master/index.php): Renders a form. This is your C in CRUD.
- [model.php](https://github.com/Corollarium/Formularium-example/blob/master/model.php): The object model, with all its fields.
- [post.php](https://github.com/Corollarium/Formularium-example/blob/master/post.php): The post form action. Only validates and renders the output.
- [Datatype_aaaaa.php](https://github.com/Corollarium/Formularium-example/blob/master/Datatype_aaaaa.php): A custom datatype. In this case it is a field that requires you to
type 'aaaaa'. This shows how you can create a new datatypem and in this case it extends the `string` datatype.
- [Renderable_aaaaa.php](https://github.com/Corollarium/Formularium-example/blob/master/Renderable_aaaaa.php): A custom renderer for the datatype. It inherits from `Renderable_string`, and just adds styling to show how to customize a field. Note that we only implement this for the HTML frontend: the Bootstrap frontend still handles the styling automatically.
17 changes: 17 additions & 0 deletions Renderable_aaaaa.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Formularium\Frontend\HTML\Renderable;

use Formularium\Field;
use Formularium\HTMLElement;

class Renderable_aaaaa extends Renderable_string
{
public function editable($value, Field $field, HTMLElement $previous): HTMLElement
{
$element = parent::editable($value, $field, $previous);
$element->get('input')[0]->setAttribute('style', 'background-color: #ccf');
return $element;
}

}
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "corollarium/formulariumapp",
"description": "Sample pure PHP app using formularium",
"type": "application",
"license": "MIT",
"version": "1.0.0",
"homepage": "https://github.com/Corollarium/FormulariumApp",
"require": {
"php": ">=7.1.0",
"corollarium/formularium": "@dev"
},
"repositories": [
{
"type": "path",
"url": "../formularium/",
"options": {
"symlink": true
}
}
],
"scripts": {
}
}
38 changes: 38 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

require('vendor/autoload.php');
require('model.php');

use Formularium\FrameworkComposer;
use Formularium\Model;

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

$model = Model::fromStruct(modelData());
?><!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Formularium pure PHP</title>
<?php echo FrameworkComposer::htmlHead(); ?>
</head>
<body>
<div class='container'>
<h1>Formularium pure PHP</h1>
<p>
This is a simple example of using <a href="https://github.com/Corollarium/Formularium">Formularium</a> in a framework agnostic way.
</p>
<form method="POST" action="/post.php">
<?php // render a form
echo $model->editable();
?>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<footer>
<a href="https://github.com/Corollarium/Formularium">Formularium on GitHub</a>
<?php echo FrameworkComposer::htmlFooter(); ?>
</footer>
</div>
</html>
67 changes: 67 additions & 0 deletions model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

use Formularium\Datatype;
use Formularium\Datatype\Datatype_integer;
use Formularium\Datatype\Datatype_string;
use Formularium\FrameworkComposer;
use Formularium\Frontend\HTML\Renderable\Renderable_number;
use Formularium\Frontend\HTML\Renderable\Renderable_string;
use Formularium\Model;
use Formularium\Renderable;

// our own datatype. You can use autoload for this, too.
require('./Datatype_aaaaa.php');
require('./Renderable_aaaaa.php');

function modelData() {
// build the model from data description. You can use a JSON file as well.
$modelData = [
'name' => 'TestModel',
'fields' => [
'myString' => [
'datatype' => 'string',
'validators' => [
Datatype_string::MIN_LENGTH => 3,
Datatype_string::MAX_LENGTH => 10,
Datatype::REQUIRED => true,
],
'extensions' => [
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'
]
],
'someInteger' => [
'datatype' => 'integer',
'validators' => [
Datatype_integer::MIN => 4,
Datatype_integer::MAX => 30,
Datatype::REQUIRED => true,
],
'extensions' => [
Renderable_number::STEP => 2,
Renderable_string::NO_AUTOCOMPLETE => true,
Renderable::LABEL => 'Some integer',
Renderable::COMMENT => 'Between 4 and 30',
Renderable::PLACEHOLDER => "Type here"
]
],
'myaaaaa' => [
'datatype' => 'aaaaa',
'validators' => [
Datatype::REQUIRED => true,
],
'extensions' => [
Renderable::LABEL => 'This is a custom datatype',
Renderable::COMMENT => 'Fill this with aaaaa',
Renderable_string::NO_AUTOCOMPLETE => true,
Renderable::PLACEHOLDER => "Type aaaaa here"
]
],
]
];
return $modelData;
}
52 changes: 52 additions & 0 deletions post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

require('vendor/autoload.php');
require('model.php');

use Formularium\FrameworkComposer;
use Formularium\Model;

// set your framework composition statically.
FrameworkComposer::set(['HTML', 'Bootstrap']);
// build the model
$model = Model::fromStruct(modelData());

// validate some data
$validation = $model->validate($_POST);

?><!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Formularium pure PHP</title>
<?php echo FrameworkComposer::htmlHead(); ?>
</head>
<body>
<div class='container'>
<h1>Formularium pure PHP</h1>
<p>
This is a simple example of using <a href="https://github.com/Corollarium/Formularium">Formularium</a> in a framework agnostic way.
</p>
<div>
<?php
if (!empty($validation['errors'])) {
foreach ($validation['errors'] as $fieldName => $error) {
echo "<p class='alert alert-danger'>$fieldName has an error: $error</p>";
}
}
else {
// get data after validation
$validated = $validation['validated'];

// render a view
echo $model->viewable($validated);
}
?>
</div>

<footer>
<a href="https://github.com/Corollarium/Formularium">Formularium on GitHub</a>
<?php echo FrameworkComposer::htmlFooter(); ?>
</footer>
</div>
</html>

0 comments on commit 8df9f3b

Please sign in to comment.