Adding the required state to radios, adds the required marker to all available options.

$form['field_options']['#states']['required'] = array(
  ':input[name="field_required[und]"]' => array('value' => 1)
);

Screenshot showing radio options set with required state with a red * next to each option.

Comments

r-mo created an issue. See original summary.

r-mo’s picture

Status: Active » Needs review
FileSize
799 bytes
r-mo’s picture

surbz’s picture

Issue summary: View changes
FileSize
35 KB
34.58 KB

Patch applies clean, Screen shots attached.

surbz’s picture

Status: Needs review » Reviewed & tested by the community
Fabianx’s picture

Version: 7.x-dev » 8.2.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)
Issue tags: +Needs tests

I am very pretty sure this needs to be fixed in D8 first. And there it can also be tested.

aerozeppelin’s picture

Just wondering, is this the fix for D8 ? https://www.drupal.org/node/1239930#comment-10371121

Fabianx’s picture

It looks different to me, can someone verify if this exists in D8 or not?

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

robpowell’s picture

Issue summary: View changes
FileSize
12.73 KB

@fabianx

It looks different to me, can someone verify if this exists in D8 or not?

I confirmed that this is still an issue on 8.1.8

I added three fields to basic page content type:

  • field_radio_state_test
  • field_state_radio_children
  • field_radio_state_children_text

Here is my form_alter hook:

function radio_state_form_node_page_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  // kint($form);

  $state_test = array(
    'required' => array(
      ':input[name="field_radio_state_test[value]"]' => array('checked' => TRUE)
    )
  );

  $form['field_state_radio_children']['widget']['#states'] = $state_test;
  $form['field_radio_state_children_text']['widget'][0]['value']['#states'] = $state_test;
  // kint($form['field_radio_state']['widget']['value']['#title']);
}
Fabianx’s picture

Issue tags: +needs backport to D7

Thanks for checking, we need to fix this in D8 first and it needs some tests.

robpowell’s picture

I was looking at the previous patch which only updates states.js. The best I can tell, it adds the styles ( red asterisk )...

In my above example I compared radio with text field. For radio fields, I found the following differences, e.target was a fieldset and the e.target.id includes '--wrapper'. For text fields, the e.target is the input and and the id matches the input. One more thing of note, the e.target.id is used to populate the label variable and in turn the find function. This will not return any results for radio fields. I quickly reviewed that this is working for checkboxes (checkboxes become required), the only difference I could determine off the bat was that checkboxes were in a div and not a fieldset.

Other than the js changes, is there any other work that needs to be completed for this? Possibly the new form fields added to validation? I am still new to Drupal but would love to help get this change in. If you could let me know the ETA on any patches and how I could assist, that would be appreciated.

nod_’s picture

Issue tags: +JavaScript
robpowell’s picture

Alright, my patch isn't going to win any awards but it does what I need it to do and hopefully shines some light on some of the issues.

sdf

Edit: add an image

robpowell’s picture

Status: Patch (to be ported) » Needs review
jrockowitz’s picture

The patch did not work for me in Drupal 8.2.x. I could not immediately 'grok' the patch to know why it did not work.

Below is the temporary CSS work-around that I am using in Webform 8.x-5.x

.form-checkboxes .form-required:after,
.form-radios .form-required:after {
  display: none;
}

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.