Next we shall talk about the evil 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:
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);
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.