2

I have in my form a type 'checkboxes' form.

    // Form for the severity checkboxes.
    $kkkk_severity_levels = variable_get('kkkk_severity_levels', array());

    // Declare empty options array.
    $kkkk_severity_options = array();

    // Loop through each severity level and push to options array for form.
    foreach (watchdog_severity_levels() as $number_select => $severity) {
      $kkkk_severity_options[$number_select] = drupal_ucfirst(
        $severity);
    }

    // Checkboxes form.
    $form['kkkk_severity_levels'] = array(
      '#type'          => 'checkboxes',
      '#title'         => t('Type of log messages'),
      '#options'       => $kkkk_severity_options,
      '#default_value' => array_values($kkkk_severity_levels),
      '#required'      => TRUE,
    );

In these lines of code the watchdog_emergency has a key 0 so when i select something from checkboxes i take selected also emergency always as it gets me out : e.g if i select 'info' with key 6 it gets me out:

6 (String, 1 characters ) 6
0 (Integer) 0
1 (Integer) 0
2 (Integer) 0
3 (Integer) 0
4 (Integer) 0
5 (Integer) 0

7 (Integer) 0 and so it selects also the 'emergency' with key as 0... How I can solve this problem?

3

That's how the form API handles multiple selections for form elements.

You can easily solve it with standard language features:

$selected = array_filter($form_state['values']['kkkk_security_levels']);
  • where i will do that line of code? – Karmen May 13 '15 at 9:06
  • In the submit handler. – Clive May 13 '15 at 9:12
  • i did it but it also selects the emergency :'( – Karmen May 13 '15 at 9:16
  • Nope, it doesn't. array_filter() will remove all elements that have a value of zero, which every element in the array you put in your question has. I suspect your issue is somewhere else – Clive May 13 '15 at 9:17

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.