function form_set_error

You are here

7 form.inc form_set_error($name = NULL, $message = '', $limit_validation_errors = NULL)
4.6 common.inc form_set_error($name, $message)
4.7 form.inc form_set_error($name = NULL, $message = '')
5 form.inc form_set_error($name = NULL, $message = '')
6 form.inc form_set_error($name = NULL, $message = '', $reset = FALSE)
8 form.inc form_set_error($name, array &$form_state, $message = '')

File an error against a form element.

Parameters

$name: The name of the form element. If the #parents property of your form element is array('foo', 'bar', 'baz') then you may set an error on 'foo' or 'foo][bar][baz'. Setting an error on 'foo' sets an error for every element where the #parents array starts with 'foo'.

$message: The error message to present to the user.

$reset: Reset the form errors static cache.

Return value

Never use the return value of this function, use form_get_errors and form_get_error instead.

Related topics

66 calls to form_set_error()
aggregator_form_category_validate in modules/aggregator/aggregator.admin.inc
Validate aggregator_form_feed form submissions.
aggregator_form_feed_validate in modules/aggregator/aggregator.admin.inc
Validate aggregator_form_feed form submissions.
block_add_block_form_validate in modules/block/block.admin.inc
block_admin_configure_validate in modules/block/block.admin.inc
book_admin_edit_validate in modules/book/book.admin.inc
Check that the book has not been changed while using the form.

... See full list

File

includes/form.inc, line 855

Code

function form_set_error($name = NULL, $message = '', $reset = FALSE) {
  static $form = array();
  if ($reset) {
    $form = array();
  }
  if (isset($name) && !isset($form[$name])) {
    $form[$name] = $message;
    if ($message) {
      drupal_set_message($message, 'error');
    }
  }
  return $form;
}

Comments

For fieldsets follow the post.

source: http://drupal.org/node/678816

Isn't it simpler to use

<?php
form_error
();
?>

for nested form fields? It automatically generates this 'element][something][else' string, just pass

<?php
form_error
($form['element']['something']['else'], $message);
?>

See as well: form_get_errors

when you are working with cck 6.3.x and you have multigroups , you must call the field in this way:

<?php
form_set_error
('field_name][i][value', t('Your message'));
?>

From http://drupal.org/node/1043614#comment-4276114

I spend you hours untill I find the link, I hope this help more people.

one example:

<?php
form_set_error
("field_medico_desde][$i][value",t('La fecha hasta no puede ser menor a la fecha desde'));
?>

I am using very complex content types with patched multigroup and conditional fields and I've been battling this for hours. This solution worked like a charm!
Example:
nodereference: "field_name][$key][nid][nid"
text fields: "field_name][$key][value"

For anyone trying to do this with a date combo (date from and to) the correct way to target the 'from' field is this :

form_set_error("field_field_name][$i][value][date",t('Error in from value'));

and the 'to' field (value2) :

form_set_error("field_field_name][$i][value2][date",t('Error in to value'));

hope this helps

When working with the Tablefield module, if you want to set an error on a specific cell, follow this example:

<?php
form_set_error
('field_name_of_your_field][0][tablefield][cell_' . $row . '_' . $column, t('Value at row @row, column @column is not valid.', array('@row' => $row, '@column' => $column)));
?>

Obviously, replace "field_name_of_your_field" with the machine name of the tablefield field from your content type.

This will "highlight" in red the specific cell at the row/column that you specify.

Hi infiniteluke,

i am working on the Themed(Table structured) form.

That table contain cells: Remove, Title, Qty, Price.

i tried the following method.

form_set_error('items][' . $val['key'] . '][qty', $message);

Result of this, QTY cell only got errored. I need the entire row will be errored.

Can you help me, how can i achieve the above scenario.

Thanks & Regards
Sarav.

something like this:

        foreach ($_SESSION['messages']["error"] as $k => $m) {
    if($m == " field is required.") {
    $_SESSION['messages']["error"][$k] = "You must read and agree to the Terms and Conditions.";
    }

You can add this to the form validation.