Custom validation for field form? I'm using below codes in Bartik theme.

I need to make: Value cannot be 4 in integer field_total

But dont know why not working.

function bartik_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  $form['#validate'][] = 'bartik_form_validate';
}
function bartik_form_validate($form, &amp;$form_state) {
  if ($form_state['values']['field_total'] == '4') {
    form_set_error('title', 'Total cannot be "4"');
  }
}


New added: clearing cache giving error:

Notice: Undefined index: field_total in bartik_form_validate() (line 161 of /var/www/dru7/themes/bartik/template.php).
The problem is that $form_state['values']['field_total'] does not exist.

First make sure that you have a field value called field_total.

Second try this code once you make sure you do $form_state['field_total']['#value'] to get the value of the field.