When a form is first rendered, there is little value in having a form class (such as Application\Form\Factory, described in the previous recipe) tied to a class that can perform filtering or validation (such as the Application\Filter\* described in the previous recipe). Once the form data has been submitted, however, interest grows. If the form data fails validation, the values can be filtered, and then re-displayed. Validation error messages can be tied to form elements, and rendered next to form fields.
Application\Form\Factory class, and add properties and setters that allow us to attach instances of Application\Filter\Filter and Application\Filter\Validator. We also need define $data, which will be used to retain the filtered and/or validated data:const DATA_NOT_FOUND = 'Data not found. Run setData()';
const FILTER_NOT_FOUND = 'Filter not found. Run setFilter()';
const VALIDATOR_NOT_FOUND = 'Validator not found. Run setValidator()';
protected $filter;
protected $validator;
protected $data;
public function setFilter(Filter $filter)
{
$this->filter = $filter;
}
public function setValidator(Validator $validator)
{
$this->validator = $validator;
}
public function setData($data)
{
$this->data = $data;
}validate() method that calls the process() method of the embedded Application\Filter\Validator instance. We check to see if $data and $validator exist. If not, the appropriate exceptions are thrown with instructions on which method needs to be run first:public function validate()
{
if (!$this->data)
throw new RuntimeException(self::DATA_NOT_FOUND);
if (!$this->validator)
throw new RuntimeException(self::VALIDATOR_NOT_FOUND);process() method, we associate validation result messages with form element messages. Note that the process() method returns a boolean value that represents the overall validation status of the data set. When the form is re-displayed following failed validation, error messages will appear next to each element:$valid = $this->validator->process($this->data);
foreach ($this->elements as $element) {
if (isset($this->validator->getResults()
[$element->getName()])) {
$element->setErrors($this->validator->getResults()
[$element->getName()]->messages);
}
}
return $valid;
}filter() method that calls the process() method of the embedded Application\Filter\Filter instance. As with the validate() method described in step 3, we need to check for the existence of $data and $filter. If either is missing, we throw a RuntimeException with the appropriate message:public function filter()
{
if (!$this->data)
throw new RuntimeException(self::DATA_NOT_FOUND);
if (!$this->filter)
throw new RuntimeException(self::FILTER_NOT_FOUND);process() method, which produces an array of Result objects where the $item property represents the end result of the filter chain. We then loop through the results, and, if the corresponding $element key matches, set the value attribute to the filtered value. We also add any messages resulting from the filtering process. When the form is then re-displayed, all value attributes will display filtered results:$this->filter->process($this->data);
foreach ($this->filter->getResults() as $key => $result) {
if (isset($this->elements[$key])) {
$this->elements[$key]
->setSingleAttribute('value', $result->item);
if (isset($result->messages)
&& count($result->messages)) {
foreach ($result->messages as $message) {
$this->elements[$key]->addSingleError($message);
}
}
}
}
}You can start by making the changes to Application\Form\Factory as described above. For a test target you can use the prospects database table shown in the How it works... section of the Chaining $_POST filters recipe. The various column settings should give you an idea of which form elements, filters, and validators to define.
As an example, you can define a chap_06_tying_filters_to_form_definitions.php file, which will contain definitions for form wrappers, elements, and filter assignments. Here are some examples:
<?php
use Application\Form\Generic;
define('VALIDATE_SUCCESS', 'SUCCESS: form submitted ok!');
define('VALIDATE_FAILURE', 'ERROR: validation errors detected');
$wrappers = [
Generic::INPUT => ['type' => 'td', 'class' => 'content'],
Generic::LABEL => ['type' => 'th', 'class' => 'label'],
Generic::ERRORS => ['type' => 'td', 'class' => 'error']
];
$elements = [
'first_name' => [
'class' => 'Application\Form\Generic',
'type' => Generic::TYPE_TEXT,
'label' => 'First Name',
'wrappers' => $wrappers,
'attributes'=> ['maxLength'=>128,'required'=>'']
],
'last_name' => [
'class' => 'Application\Form\Generic',
'type' => Generic::TYPE_TEXT,
'label' => 'Last Name',
'wrappers' => $wrappers,
'attributes'=> ['maxLength'=>128,'required'=>'']
],
// etc.
];
// overall form config
$formConfig = [
'name' => 'prospectsForm',
'attributes' => [
'method'=>'post',
'action'=>'chap_06_tying_filters_to_form.php'
],
'row_wrapper' => ['type' => 'tr', 'class' => 'row'],
'form_wrapper' => [
'type'=>'table',
'class'=>'table',
'id'=>'prospectsTable',
'class'=>'display','cellspacing'=>'0'
],
'form_tag_inside_wrapper' => FALSE,
];
$assignments = [
'first_name' => [ ['key' => 'length',
'params' => ['min' => 1, 'max' => 128]],
['key' => 'alnum',
'params' => ['allowWhiteSpace' => TRUE]],
['key' => 'required','params' => []] ],
'last_name' => [ ['key' => 'length',
'params' => ['min' => 1, 'max' => 128]],
['key' => 'alnum',
'params' => ['allowWhiteSpace' => TRUE]],
['key' => 'required','params' => []] ],
'address' => [ ['key' => 'length',
'params' => ['max' => 256]] ],
'city' => [ ['key' => 'length',
'params' => ['min' => 1, 'max' => 64]] ],
'state_province'=> [ ['key' => 'length',
'params' => ['min' => 1, 'max' => 32]] ],
'postal_code' => [ ['key' => 'length',
'params' => ['min' => 1, 'max' => 16] ],
['key' => 'alnum',
'params' => ['allowWhiteSpace' => TRUE]],
['key' => 'required','params' => []] ],
'phone' => [ ['key' => 'phone', 'params' => []] ],
'country' => [ ['key' => 'in_array',
'params' => $countries ],
['key' => 'required','params' => []] ],
'email' => [ ['key' => 'email', 'params' => [] ],
['key' => 'length',
'params' => ['max' => 250] ],
['key' => 'required','params' => [] ] ],
'budget' => [ ['key' => 'float', 'params' => []] ]
];You can use the already existing chap_06_post_data_config_callbacks.php and chap_06_post_data_config_messages.php files described in the previous recipes. Finally, define a chap_06_tying_filters_to_form.php file that sets up autoloading and includes these three configuration files:
<?php require __DIR__ . '/../Application/Autoload/Loader.php'; Application\Autoload\Loader::init(__DIR__ . '/..'); include __DIR__ . '/chap_06_post_data_config_messages.php'; include __DIR__ . '/chap_06_post_data_config_callbacks.php'; include __DIR__ . '/chap_06_tying_filters_to_form_definitions.php';
Next, you can create instances of the form factory, filter, and validator classes:
use Application\Form\Factory;
use Application\Filter\ { Validator, Filter };
$form = Factory::generate($elements);
$form->setFilter(new Filter($callbacks['filters'], $assignments['filters']));
$form->setValidator(new Validator($callbacks['validators'], $assignments['validators']));You can then check to see if there is any $_POST data. If so, perform validation and filtering:
$message = '';
if (isset($_POST['submit'])) {
$form->setData($_POST);
if ($form->validate()) {
$message = VALIDATE_SUCCESS;
} else {
$message = VALIDATE_FAILURE;
}
$form->filter();
}
?>The view logic is extremely simple: just render the form. Any validation messages and values for the various elements will be assigned as part of validation and filtering:
<?= $form->render($form, $formConfig); ?>
Here is an example using bad form data:

Notice the filtering and validation messages. Also notice the bad tags:
