1

I have a "checkboxes" field that allows the user to select unlimited options. How do I check if at least a specific value has been selected?

I am using the Rules module, and when it checks if the 'field has value' it combines all the options. Right now, if both the values "b" AND "c" are selected, then the action (which checks for the value "b") won't be executed.

4

I ended up creating a "Check a Truth Value" condition with the Rules module. The code below is the one I used.

if ($node->field_area_51[0]['value']) {
  foreach ($node->field_area_51 as $key => $value) {
    if ($value[value] == 'b') {
      return TRUE;
    }
  }
}
1

I would build a hook_nodeapi validate (D6) or a hook_node_validate (D7) to check if "b" is selected, use dsm to inspect the values of $node. I supposed you're working with the content type page: $node->type == 'page' and you field is named field_options.
The values are defined like this:

a|A option
b|B option
c|C option

Code for D7:

function mymodule_node_validate($node, $form) {
  dsm($node);
  $b_selected = FALSE;
  if ($node->type == 'page') {
    foreach($node->field_options[$node->language] as $option) {
      if ($option['value'] == 'b') {
        $b_selected = TRUE;
      }
    }
    if (!$b_selected) {
      form_set_error('field_options', t('Please select at least option B'));
    }
  }
}

PS: I'm not sure about the form_set_error for fields

  • Thanks tostinni. Is it bad to use the Rules module for something like this? I need content to be created based on whether or not 'b' is selected. I also need certain fields to be prepopulated as well. That's why I opted to use the Rules module as opposed to writing a somewhat complex function with lots of hooks. – scott Jul 25 '11 at 1:00
  • I'm just not familiar enough with rules and thought this was a perfect exemple for using hook_node_validate ;) – tostinni Jul 25 '11 at 1:43
0

Sorry to say, but your custom rule works as implemented: only if value "b" is selected, the list of selected values is equal to ... "b", while if value "b" AND "c" are selected, then the list of selected values is NOT equal to ... euh ... "b".

To get your rule to work like you want it to work, then you should use these Rules Conditions (in the specified order):

  • Entity has field, for your "checkboxes" field.
  • List contains item, whereas:
    • the list is also about that same "checkboxes" field (since your field allows multiple values, it is also considered as a list).
    • the item you want to check for (if it is contained in the list) is "b".

With those 2 Rules Conditions, your Rules Actions should be perform (1) if only value "b" was selected, but (2) also if value "b" and value "c" were both selected.

For an example Rule (in rules export format) in which you can see this at work, refer to the answer to "How to check for product taxonomy term in Rules actions (Ubercart)". In that case it's about a list of taxonomy terms, but the idea is the same.

PS: even though CCK is rather D6-ish, the same kind of mistake can happen in D7 (which is what my answer is about).

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.