0

I need to disable other radio button(s) when [#default_value] => Inactive as in below code it is? I tried with different ways but unsuccessful. Kindly let me know how to do that?

PHP CODE

foreach($form['field_complaint_status'] as $key=>$val)
{
    echo '<pre>'; print_r($val['field_complaint_status']); exit;
}

Output

Array
(
    [#type] => container
    [#attributes] => Array
        (
            [class] => Array
                (
                    [0] => field-type-list-text
                    [1] => field-name-field-complaint-status
                    [2] => field-widget-options-buttons
                )

        )

    [#weight] => 13
    [#tree] => 1
    [#language] => und
    [und] => Array
        (
            [#entity] => stdClass Object
                (
                    [vid] => 544
                    [uid] => 754
                    [title] => Complaint/Feedback
                    [log] => 
                    [status] => 1
                    [comment] => 1
                    [promote] => 1
                    [sticky] => 0
                    [nid] => 544
                    [type] => complaint_or_feedback
                    [language] => en
                    [created] => 1492408487
                    [changed] => 1492587955
                    [tnid] => 0
                    [translate] => 0
                    [revision_timestamp] => 1492587955
                    [revision_uid] => 754
                    [field_type] => Array
                        (
                            [und] => Array
                                (
                                    [0] => Array
                                        (
                                            [value] => Complaint
                                        )

                                )

                        )

                    [field_complaint_status] => Array
                        (
                            [und] => Array
                                (
                                    [0] => Array
                                        (
                                            [value] => Inactive
                                        )

                                )

                        )
                    [cid] => 0
                    [last_comment_timestamp] => 1492408487
                    [last_comment_name] => 
                    [last_comment_uid] => 754
                    [comment_count] => 0
                    [menu_node_links] => Array
                        (
                        )
                    [picture] => 0
                )

            [#entity_type] => node
            [#bundle] => complaint_or_feedback
            [#field_name] => field_complaint_status
            [#language] => und
            [#field_parents] => Array
                (
                    [0] => field_complaint_status
                    [1] => 0
                )

            [#columns] => Array
                (
                    [0] => value
                )

            [#title] => 
            [#description] => 
            [#required] => 1
            [#delta] => 0
            [#type] => radios
            [#default_value] => Inactive
            [#options] => Array
                (
                    [Active] => active
                    [Inactive] => inactive
                )

            [#value_key] => value
            [#element_validate] => Array
                (
                    [0] => options_field_widget_validate
                )

            [#properties] => Array
                (
                    [filter_xss] => 1
                    [strip_tags] => 
                    [strip_tags_and_unescape] => 
                    [empty_option] => 
                    [optgroups] => 
                )

            [#after_build] => Array
                (
                    [0] => field_form_element_after_build
                )

        )

    [#access] => 1
)
4
  • Where is you radio button code?
    – Suraj
    Apr 19, 2017 at 9:18
  • I created it using manage field of a content type. I didn't code it. Apr 19, 2017 at 9:22
  • what is the field name??
    – Suraj
    Apr 19, 2017 at 9:25
  • its name is field_complaint_status as I already mention in my question. Apr 19, 2017 at 9:28

2 Answers 2

2

You can use the #states property to achieve that. The #states property can be applied to all Form API elements.

  $form['student_type'] = array(
    '#type' => 'radios',
    '#options' => array(
      'high_school' => t('High School'),
      'undergraduate' => t('Undergraduate'),
      'graduate' => t('Graduate'),
    ),
    '#title' => t('What type of student are you?'),
  );
  $form['high_school'] = array(
    '#type' => 'fieldset',
    '#title' => t('High School Information'),
    // This #states rule says that the "high school" fieldset should only
    // be shown if the "student_type" form element is set to "High School".
    '#states' => array(
      'visible' => array(
        ':input[name="student_type"]' => array('value' => 'high_school'),
      ),
    ),
  );

References : states

form_example_states_form

3
  • Please re-read my question I asked about disable other values (radio buttons) of the same field as above mentioned [#default_value]. [#default_value] means the same field. Your answer is referring to show/hide fields as Conditional Fields works. Apr 21, 2017 at 4:58
  • Can you provide image of the form?
    – developer
    Apr 21, 2017 at 8:54
  • I have figured out. See my answer below Apr 21, 2017 at 10:27
0

I have done with JS, See code below

jQuery(".inactive input").each(function(index, element) {
    jQuery(this).attr("disabled",true);
});

Your Answer

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

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