{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 |
|---|---|---|
|
padPosted ( 'contact' ) is true when
this form came back, so a page with two forms knows which one did.label= writes a
label tied to the field, value= the value before anything was posted, and every
other item is an attribute of the field, as {attrs} writes it - required,
rows=6, placeholder='...'.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 |
|---|---|---|
|
|
|
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.
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 |
|---|---|---|
|
|
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.
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 |
|---|---|
|
|
action= or method='get' and rules on a file field are errors -
nothing would check them.padValidate adds its messages to those of the template.padFormFailed ( 'contact' ) is true when the form came back and broke its
rules.