{form}, {input} and {textarea} write the fields of a form. When the form comes back with mistakes in it, each field shows again what was typed - escaped - and the message for its mistake beside it. padValidate in the page's PHP finds the mistakes.

PHP
HTML
fragments/forms_1.pad
Result
{form 'contact'}
{input 'email', type='email', label='E-mail', required}
{textarea 'message', label='Message', rows=3}
<button>Send</button>
{/form}

Validation

padValidate ( $rules ) checks the posted values and answers one message per field that breaks a rule, keyed by the field - empty when all is well. A second argument checks other data; a third replaces messages, per field ('email') or per field and rule ('email.required'):

PHP
fragments/forms_2.php
HTML
fragments/forms_2.pad
Result
<?php

$errors
= padValidate (
[
'email' => 'required|email',
'message' => 'required|max:20' ],
[
'email' => 'ann@',
'message' => '' ] );

?>
<ul>
{errors}<li>{$errors}</li>{/errors}
</ul>
  • Email must be a valid e-mail address
  • Message is required

The rules: required, email, url, numeric, integer, min:n and max:n (a length, or a value with numeric or integer), in:a,b,c, regex:/.../, same:other_field, accepted and date. An empty field is checked for required and accepted only. A rule name nothing knows is an error.

After a failed post

The fields of the form that came back show the message beside them, in the words of their own label, tied to the field with aria-invalid and aria-describedby so a screen reader reads them together. Here the post is pretended in the PHP, the way a visitor's would arrive:

PHP
fragments/forms_3.php
HTML
fragments/forms_3.pad
Result
<?php

// As if the form had been posted with these values.

$_SERVER ['REQUEST_METHOD'] = 'POST';
$_POST = [ 'email' => 'ann@', 'message' => 'Hello' ];

$errors = padValidate ( [ 'email' => 'required|email', 'message' => 'required' ] );

?>
{input 'email', type='email', label='E-mail'}
{textarea 'message', label='Message', rows=3}
E-mail must be a valid e-mail address

After a post that passes, store it and send the visitor on with padRedirect (): a refresh of the page they land on does not post a second time.

Rules in the template

The rules can stand on the fields themselves: rules='required|email' on an {input} or a {textarea}. They are read from the page's template before its PHP runs, and a post of the form is checked against them there - padPosted ( 'contact' ) is true only for a post that kept them, so the PHP acts on a good post alone. A post that broke one renders the form again, refilled, with the messages beside the fields and the form's error= above them (error alone says "Please correct the errors below."):

PHP
fragments/forms_4.php
HTML
fragments/forms_4.pad
<?php

// Runs after the post was checked against the rules of forms_4.pad, and acts only when
// it kept them; a post that broke one renders the form again, with its messages.

if ( padPosted ( 'contact' ) ) {

padFlash ( 'Thank you for your message.' );

padRedirect ();

}

?>
{form 'contact', error='Please correct the errors below.'}
{input 'email', type='email', label='E-mail', rules='required|email'}
{textarea 'message', label='Message', rows=3, rules='required|max:2000'}
<button>Send</button>
{/form}