4.6.x common.incform_set_error($name, $message)
4.7.x form.incform_set_error($name = NULL, $message = '')
5.x form.incform_set_error($name = NULL, $message = '')
6.x form.incform_set_error($name = NULL, $message = '',$reset= FALSE)
7.x form.incform_set_error($name = NULL, $message = '',$limit_validation_errors= NULL)

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

67 calls to form_set_error()

File

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

sourabh.iitm’s picture

For fieldsets follow the post.

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

sourabh.iitm’s picture

Isn't it simpler to use  form_error();  for nested form fields? It automatically generates this 'element][something][else' string, just pass  form_error($form['element']['something']['else'], $message);

kenorb’s picture

See as well: form_get_errors

sourabh.iitm’s picture

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'));
?>
sourabh.iitm’s picture

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"

sourabh.iitm’s picture

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

infiniteluke’s picture

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

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.

sourabh.iitm’s picture

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.

sourabh.iitm’s picture

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.

sourabh.iitm’s picture

Hi, I found very helpful this documentation and thanks to all for the suggestions.

Now I try to set the following code:

function _form_alter(&$form, &$form_state, $form_id) {

//DEBUG VALUES
dsm($form_id);
dsm($form);

//FORM VALIDATION
form_set_error('][0][tablefield][cell_' . $row . '_' . $column, t('Value at row @row, column @column is not valid.', array('@row' => $row, '@column' => $column)));
}

But in the edit page i got this:
"Error message - Value at row , column is not valid."

Even if I change the $row and the $column variable to a fixed value.

Something goes wrong on my approach....
Any suggestions will be much appreciate.

Thanks in advance.
Best regards.

Add new comment

shipingzhong

Buggy or inaccurate documentation? Please file an issue instead of commenting here. Need support? Need help programming? Connect with the Drupal community.

来自  https://api.drupal.org/api/drupal/includes%21form.inc/function/form_set_error/6.x