Forms and validation

You should validate all input data entered in forms. In Zotonic you create forms by writing plain HTML. You can attach one or more validators to each input element. Validators define acceptable values for the input data.

Let’s say you have a required input field ‘title’. To make sure some text is entered in it, attach the presence validator:

<input type="text" id="title" name="title" />
{% validate id="title" type={presence} %}

The validated form field is available to Erlang code using the z_context:get_q_validated/2 function:

Title = z_context:get_q_validated(<<"title">>, Context).

Client- and server-side

Zotonic’s validators work both client- and server-side:

  • JavaScript prevents the form from being submitted until the input data conforms to the validators.
  • All validation is done on the server as well, which protects against users bypassing the validation checks in their browser.

Validators

Zotonic comes with some commonly needed validators:

If you need to implement custom validation logic, use the postback validator. For JavaScript-only custom validation, use the custom validator.

acceptance
See also
confirmation
See also
custom
See also
date
Validate input date against a given date format.
email
See also
email_unique
See also
format
See also
length
See also
name_unique
See also
numericality
See also
postback
See also
presence
See also
username_unique
See also

Icons Developer Guide Search

Referred by

Modules

Modules are the building blocks of Zotonic. They add functionality to your Zotonic website such as:

name_unique

See also

hasedge

A validator to check if a resource has a certain number of edges with a predicate.