0

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(&$form, &$form_state, $form_id) {
  $form['#validate'][] = 'bartik_form_validate';
}
function bartik_form_validate($form, &$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).
  • 2
    Why not create a custom module and put all your custom codes there instead of using theme? – saadlulu Feb 25 '13 at 14:40
  • clear your cache – Petro Popelyshko Feb 25 '13 at 14:53
  • @saadlulu, This for learning purposes. – apasajja Feb 25 '13 at 15:58
  • @neok, clearing cache giving error mentioned above – apasajja Feb 25 '13 at 15:59
  • That is saying there is no value field_total, you need ensure that the form_state actually contains that value as field_total because it may not actually be named that – chadpeppers Feb 25 '13 at 16:05
0

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.

  • Of course I have field field_total – apasajja Feb 26 '13 at 1:13
  • Changing the code still giving an error. Changing field name also not helps – apasajja Feb 26 '13 at 1:28
  • try using $form instead of $form_state. like this. $form['field_total']['#value']. and show us the error you got. – saadlulu Feb 26 '13 at 7:10
  • Use $form['field_total']['#value'] can stop the error, but it not works too. – apasajja Feb 26 '13 at 12:40
  • var_dump($form['field_total']['#value']);exit(); on the first line of your function, copy and paste the result and show us. please! – saadlulu Feb 26 '13 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.