5

I have a Form API checkbox that can be enabled and disabled by another ("parent") one:

$form['my_checkbox']['#states'] = array(
  'enabled' => array(
    ':input[identifier="parent"]' => array('checked' => TRUE),
  ),
);

Works like a charm, but now I want it to get cleared if it is dizabled. So I made my code look like this:

$form['my_checkbox']['#states'] = array(
  'enabled' => array(
    '[:input[identifier="parent"]' => array('checked' => TRUE),
  ),
  'unchecked' => array(
    ':input[identifier="self"]' => array('disabled' => TRUE),
  ),
);

Sadly, this also automatically checks that checkbox when it becomes enabled, and that's not what I want - I want to give my user an option, but not suggest him to check that field at all.

Can this "one way" action be achieved with '#states'? If so, how? Or is it always two way deal, and there is no option to uncheck on disabling without checking on enabling?

Note: I tried to use both disabled and enabled of self, and checked & unchecked of parent, with the same results.

I know I can achieve it with behavior, that's how I have it done now, but states api is there to avoid behavior's complexity and my use case seems pretty simple.

  • 1
    Legal reason: it is illegal in some jurisdictions to auto-check "I agree to ... " checkboxes for users. Not to mention it might be immoral to do so. And for clarity if someone does not agree to give me any data, I don't want to make it look like there is still checked some extra use of it. – Mołot Sep 30 '13 at 12:16
4

Turns out that with current implementation of states it is not possible. When condition gets true, state is applied, and when it gets false, state gets removed. No way around it so far, not without hacking core, so it's more feasible to write custom behaviors instead.

| improve this answer | |
  • If anyone wants to provide a better answer, or more elaborated answer saying essentially the same, and get a hat for stealing checkmark, feel free and encouraged to do it! Promise not to keep it for myself. – Mołot Dec 24 '13 at 12:12
-2

it's possible create states with unchecked.

See https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_process_states/7

[]'s

| improve this answer | |
  • Sorry, but I already know this, and I don't see how this link can answer my question. Also there is not too much text to it to explain what you meant. Are you sure you're answering the specific case I asked about? – Mołot Jun 4 '14 at 6:15

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.