Specify field names for error messages
Validatious aims to generate reasonable error messages by resolving field names. There's three ways Validatious can find the name of a field:
-
The default is to find the label associated with the field through the
input/select/textareaidattribute and thelabelforattribute. TheinnerHTMLof thelabelwill be used as the field name. -
If the
labelelement has a non-emptytitleattribute, this will be used instead of theinnerHTML. -
If no
labelcan be found, theidattribute of the field element will be used. If noidis found either, thenameattribute is used.
In addition to these, you may also set the name of a field programmatically
through v2.$f(id).setName(newName). Doing this will override the
search described above. From v2.Field objects, you can set the
name through field.element.setName(name);.
Examples
The following example shows the default behaviour with a label without the
title attribute. If you post the form with an empty name or only
a single character you'll see how "Your name" is used in both the (default)
error messages.
The following example shows how setting the title attribute on
the label allows you to use a different field name in the error
messages than what is displayed in the label.
The last example shows how Validatious will use the id of the
field when no label can be found. Notice that the label is there,
but the for/id connection is wrong.
Next: Multilingual messages | Back to Core API features index