4.7.x form.inc form_error(&$element, $message = '')
5.x form.inc form_error(&$element, $message = '')
6.x form.inc form_error(&$element, $message = '')
7.x form.inc form_error(&$element, $message = '')

Flags an element as having an error.

Related topics

28 calls to form_error()
date_validate in includes/form.inc
Validates the date type to prevent invalid dates (e.g., February 30, 2006).
element_validate_integer in includes/form.inc
Form element validation handler for integer elements.
element_validate_integer_positive in includes/form.inc
Form element validation handler for integer elements that must be positive.
element_validate_number in includes/form.inc
Form element validation handler for number elements.
field_default_form_errors in modules/field/field.form.inc
Transfer field-level validation errors to widgets.

... See full list

File

includes/form.inc, line 1704
Functions for form and batch generation and processing.

Code

function form_error(&$element, $message = '') {
  form_set_error(implode('][', $element['#parents']), $message);
}

Comments

DrupalFrank’s picture

I've run into the situation several times now where the $form markup had a few fields with no #title value and were marked as #required=TRUE. On a complex form while developing it is not always clear why the form fails to submit when there are no visual queues -- this is the case with $message = ''. Instead, can we make the default parameter value $message = 'Bad value'? That makes the root cause something easier to locate.

meustrus’s picture

Form fields with #required=TRUE must have a #title to validate properly, as you have discovered. The best way to mitigate this unexpected issue is to give every element a #title, and if you din't want the #title rendered, also give the element a #title_display='invisible'. This way the #title will only be used internally, such as for form errors, and will not be displayed to the user as a field label.