SVF stands for Self Validating Form. It's a light weight framework: it only has a handful of PHP files to include. The purpose is to ease writing HTML form with validation code. It handles the displaying and the validation in one fell swoop.
Download version 0.1. The software comes with a bunch of PHPunit tests that are not included. The test scripts can be downloaded here.
This code displays a valid form with no validation. It creates a form object and add three input fields using the function add. You can put any free form text in between the fields by using the function output. In the example a break is inserted between each field.
The last function end_printf terminates the form and prints it out.
<?php include("form.php"); $form = new Form("post", "example1.php", array()); $form->add ("input", "login", array ("label" => "Login")); $form->output("<br>"); $form->add ("input", "password", array ("label" => "Password")); $form->output("<br>"); $form->add("input", "submit", array ("value" => "Submit", "type" => "submit",)); $form->end_printf(); ?>
The form looks like this, it doesn't look great but read about templates below.

Most of the time you need to validate user's input. You do this in SVF with validators. They are array with the name of a function coupled with an error message. SVF will go through each validator and call them with the input string as an argument. If one of the validator fails it will stop and display an error message.
In the example below the if the function function_check_date_time returns false an error message will be displayed "Please enter publishing date".
Notice a new argument when creating the form called mark. This will be the text that will be displayed for each required field.
The first field in the example has the required flag set to true. So an asterisk will be displayed next to the label.
Last we call the form valid method, if it returns true we can process the data entered by the user otherwise we display the form that may contain some error messages.
<?php include "form.php"; include "function.php"; $form = new Form("post", "example2.php", array ("mark" => "<em>*</em>")); $form->add("input", "publishing_date", array ("label" => "Publishing Date", "value" => "{$publishing_date}", "required" => true, "validators" => array (array ("not_empty", "Please enter the publishing date"), array ("function_check_date_time", "The date format is DD-MM-YYYY HH:MM.")))); $form->output("<br>"); $form->add("input", "end_subscription", array ("label" => "End of subscription", "value" => "{$end_subscription}", "validators" => array (array ("empty_or_check_date", "The end of subscription format is JJ-MM-AAAA")))); $form->output("<br>"); $form->add("textarea", "article", 3, 80, array ("label" => "Article", "value" => "{$article}")); $form->output("<br>"); $form->add("input", "submit", array ("value" => "Submit", "type" => "submit",)); if ($form->valid()) { // process data debug($_POST); } else { // print form $form->end_printf(); } ?>
Here is the form when it's rendered, the error message is displayed as the entry is erroneous.

So far the form haven't looked very good. The usual technique to improve the look of a form is to use a table. Templates are very handy as they will automatically be inserted between each fields.
Here is a basic template:
<?php $template_table = ' if ($error) { $error_msg = <<<EOF <tr> <td></td> <td class="error"> {$error} </td> </tr> EOF ; } $output= <<<EOF <tr> <td> {$label} </td> <td> {$widget} </td> </tr> {$error_msg} EOF ;'; ?>
The template variable is a string that is evaluated at run time. If an error message happens at the current field the error variable will contain the error message. The variable output contains the format you want to print between each fields.
The template create data row and eventually prints another row with the class error which happens to be red. You can use template to create sophisticated forms and put the error message where you like.
Here the first field is a file input. Note that validator function for file input take a PHP file array as an argument. See how the function function_file_exists differs from function_check_date_time in function.php.
<?php include "form.php"; include "function.php"; include "template.php"; print '<style type="text/css">td.error { color: red; }</style>'; $form = new Form("post", "example3.php", array ("template" => $template_table, "mark" => "<em>*</em>")); $form->output("<table>\n"); $form->add("input", "picture", array ("label" => "Picture", "type" => "file", "required" => true, "validators" => array (array ("function_file_exists", "Please choose a picture"), array ("function_check_image", "The picture file is not a jpeg."), array ("function_file_valid", "Special character and accents are not allowed.")))); $form->add("input", "name", array ("label" => "Name")); $form->add("input", "submit", array ("value" => "Submit", "type" => "submit")); $form->output("</table>\n"); if ($form->valid()) { // process data and file debug($_POST); debug($_FILES); } else { // print form $form->end_printf(); } ?>
Here is the output when giving a file that's is not a jpeg. Note that the fields are nicely lined up and the error message is red. The template is achieving all this.

Radio buttons are a special kind of input fields. They are handled by using the radio widget. Each radio has a label and value attributes. Optionally it can have the checked attribute set to true so that it's checked when displaying the form.
<?php include("form.php"); $form = new Form("post", "example4.php", array()); $form->add("radio", "rubrique", array ("label" => "Choose a color :", "required" => true, "elements" => array (array ("label" => "Red", "value" => "red", "checked" => true), array ("label" => "Green", "value" => "green"), array ("label" => "Blue", "value" => "blue")))); $form->output("<br>"); $form->add("input", "submit", array ("value" => "Submit", "type" => "submit")); if ($form->valid()) { // process data and file debug($_POST); } else { // print form $form->end_printf(); } ?>
Notice that the color red is checked.
