Notes on codes, projects and everything

My evil evil form and database library

Next we shall talk about the evil form library.

Form Library

I want a form library that is simple, actually most part of my form library is similar to what other scripts offer. However, mine has some integration with the above mentioned database library. For example, consider the following form template:

"form": {
"method": "post",
"name": "post"
"field": {
"title": {
"required": true,
"widget": {
"type": "text"
"validation": {
"reference": {
"required": true,
"widget": {
"type": "lookup",
"schema": "post.json",
"query": "widget",
"field": {
"value": "value",
"display": "name"
"validation": "check"

Nothing much to expect, the schema should be self-explainable enough. The only part that may be odd would be the lookup field named reference. It is basically a select widget, that grabs the option from a schema stored in post.json (json_reader() function should return a function that can turn this filename to proper assoc array). The “query” key is the actual statement in the schema expected to return the result with “field” option describes the value and display columns.

Unlike the database library, the form library need no explicit creation, just read the above schema file and return a respective associative array. Therefore it can be as simple as this

$form = json_decode($file_of_form_schema, TRUE);

However, if CSRF protection is needed, just pass the form array to the init function, as follows

$form = \coolsilon\form\init(json_decode($file_of_form_schema, TRUE));

What init() does is that a new hidden token field is added to the form. Next we would want to send the form for validation, as follows:

$generator = \coolsilon\database\relation\generator($pdo, json_reader());
$validation_result = \coolsilon\form\validate($form, $generator, \coolsilon\form\input\read($_POST));

The generator is needed just in case certain form needs access to the database for validation purpose. Then read() function removes the ‘field-‘ prefix to the array keys so that the form library can recognize them properly. If you data comes without the ‘field-‘ prefix you can skip this step.

$data = array('title' => 'foo', 'reference' => 1);
$validation_result = \coolsilon\form\validate($form, $generator, $data);

The function validate() itself would return TRUE if the input data passes all validation. However, if it fails then an array containing the error messages will be returned. The current form of a function that does validation is as follows. While the library is still under construction, I am not gonna talk much about the validation function itself for now.

Getting the actual markup of the form is easy, just pass in the parameters as follows:

// a blank form
echo \coolsilon\form\markup($form, $generator);
// a form with some default data
echo \coolsilon\form\markup($form, $generator, array('title' => 'foo'));
// a form with previous submitted data that is invalid
echo \coolsilon\form\markup($form, $generator, \coolsilon\form\input\read($_POST), $validation_result);

Why procedural?

I know PHP has great OOP support starting in version 5+. However, I start to appreciate functional programming more these days. Especially after PHP 5.3+ I can start abusing anonymous function and closure, it really change my way of writing codes and even further irritate my peers more. However, there are still rooms for improvement as I really doubt if I would remember what the code does if left for a couple of days.

Another problem with the current design is the namespacing problem. I am not sure if I like this feature introduced in recent PHP releases. However, after using it I really doubt so. Look at those ridiculous \coolsilon\database\… function calls. I think sooner or later I will re-organize the code to depend less on it, at least not multiple layers of namespaces. Other than that I am quite happy with the current state.

The actual library can be found here, feel free to try playing with it. Just a reminder, a lot of things may be broken right now.

leave your comment

name is required

email is required

have a blog?

This blog uses scripts to assist and automate comment moderation, and the author of this blog post does not hold responsibility in the content of posted comments. Please note that activities such as flaming, ungrounded accusations as well as spamming will not be entertained.


@kma took me quite a while to figure out who you are
nah, it is still better than writing PHP like Java, no? I consider them just as evil

Choon-Siang Lai

This is simply madness ! Stop writing PHP like Javascript. 😉

Click to change color scheme