0

I need to show a validation error message when a radio button is clicked but none of the checkbox is checked. The checkbox field is optional in content type. Should be required when radio button is clicked. How can it implement this programaticaly. Not interested to add conditional field module for a single requirement.

3
  • is this a custom form? Or what kind of form?
    – thumbson
    Commented Feb 16, 2015 at 14:50
  • hook_form_alter()
    – Clive
    Commented Feb 16, 2015 at 14:55
  • Its a content type.
    – drup
    Commented Feb 16, 2015 at 14:56

2 Answers 2

1

Have you try the field API hook_field_validate?

1

In form alter use this code

function my_module_form_alter(&$form, &$form_state, $form_id) {
      $radio = $form['#node']->field_radio['und'][0]['value'];

      if($radio == 1){//if 
         $form['field_checkbox']['und']['0']['value']['#required'] = TRUE;
         //OR
         $form['field_checkbox']['und']['0']['#required'] = TRUE;
      }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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