{input}

A form field with its label that refills from what its form sent and shows the error validation found for it.

Forms and security Single tags

Syntax

{input 'name'}
{input 'email', type='email', label='E-mail', value='', id='mail', placeholder='you@example.org', required}
{input 'terms', type='checkbox', label='I agree', value='yes', checked}
{input 'email', type='email', label='E-mail', rules='required|email'}

Parameters

name
The field name - required. user[email] and tags[] are found where PHP files them; the id is the name with what an id cannot hold made a dash.

Options

type
The input type, text by default: email, number, checkbox, radio, hidden, password, file, submit ...
label
A <label for> before the field - after a checkbox or radio - and the word its error message uses.
value
The value before anything was sent; for a checkbox or radio its own value (on by default).
id
The id, by default the name.
checked
Bare option: a checkbox or radio is checked before a post.
rules
The field's rules - required|email|max:80 - read from the page's own template and checked before its PHP runs. Only in a named {form} of the page itself.

Description

{input 'email', type='email', label='E-mail'} writes a <label for> and an <input> with the name and an id - the name with every character an id cannot hold made a dash, so user[email] gets the id user-email. The name is required: a field without one is an error under the strict check.

The tag takes type, label, value, id, checked and rules itself; every other item is an attribute of the input, written as {attrs} writes it - a bare word such as required is a bare attribute, a false value leaves the attribute out.

Before a post a field shows value=. After a post of its own {form} it shows the posted value, escaped, found where PHP files the name (user[email] is $_POST['user']['email']). A checkbox or radio is checked when the posted value is its own value - before a post when checked is given - and its label comes after it. A password or file field is never refilled; a submit, button, reset or image input has no label and no error.

When padValidate - or the field's own rules= - found something wrong, the message follows the field in <span class="error" id="email-error">, worded with the field's label, and the field gets aria-invalid="true" and aria-describedby, so a screen reader says the message with the field. A file field shows why padUpload refused its file the same way. Outside a {form} the tag still writes the field, but it never refills.

Examples

Field types

A label before a text field, after a checkbox or radio; value= is what shows before anything was sent.

Templateexamples/input/1-types.pad
{form 'order', method='get'}
  {input 'name', label='Name', value='Ann'}
  {input 'size', type='radio', id='size-s', label='Small', value='S'}
  {input 'size', type='radio', id='size-l', label='Large', value='L', checked}
  {input 'gift', type='checkbox', label='Gift wrap', value='yes'}
  {input 'step', type='hidden', value='2'}
{/form}
Resultopen alone

Other items are attributes

Every item the tag does not take itself goes on the input, a bare word as a bare attribute.

Templateexamples/input/2-attributes.pad
{set $busy = FALSE}
{input 'qty', type='number', label='Quantity', value=1, min=1, max=10, required, disabled=$busy}
{input 'user[email]', type='email', label='E-mail', placeholder='you@example.org', autocomplete='email'}
Resultopen alone

The message beside the field

After a post padValidate found wrong, the field shows the posted value and its message, tied to it for a screen reader.

Templateexamples/input/3-errors.src
{# contact.php: if ( padPosted ( 'contact' ) ) $errors = padValidate ( [ 'email' => 'required|email' ] ); #}
{form 'contact'}
  {input 'email', type='email', label='E-mail'}
  <button>Send</button>
{/form}

{# after posting "ann@" the field renders as:
<label for="email">E-mail</label><input type="email" name="email" id="email" value="ann@"
  aria-invalid="true" aria-describedby="email-error"><span class="error" id="email-error">E-mail must be a valid e-mail address</span> #}

See also

  • {form} A form that posts back to the page, carrying the session's CSRF token and its own name, its fields refilling when it came back.
  • {textarea} A text area with its label that refills from what its form sent and shows the error validation found for it.
  • {attrs} Writes HTML attributes from values - quoted and escaped, a true boolean attribute bare, a false one left out.
  • {validator} The browser's checker of the validation rules as one inline script, for a form a component draws.