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.

CommentFileSizeAuthor
#70 2731991-64.patch2.88 KBandresgmh
#69 Screen Shot 2023-02-01 at 11.54.16 am.png4.01 KBpameeela
#63 interdiff_61-63.txt2.58 KBvsujeetkumar
#63 2731991-63.patch2.77 KBvsujeetkumar
#62 interdiff_61-62.txt637 bytesvsujeetkumar
#62 2731991-62.patch2.6 KBvsujeetkumar
#61 2731991-61.patch2.71 KBvsujeetkumar
#54 Screenshot 2020-06-03 at 12.41.32 PM.png40.89 KBsharma.amitt16
#50 2731991-50.patch2.7 KBhimanshu_sindhwani
#48 radio-required-test.png206.95 KBhimanshu_sindhwani
#48 radio-required-code.png59.21 KBhimanshu_sindhwani
#47 2731991-47.patch4.02 KBpiggito
#42 2731991-42.patch4.01 KBaudacus
#38 interdiff-34-38.txt1.1 KBilya.no
#38 2731991-38.patch3.83 KBilya.no
#34 interdiff-28-34.txt7.31 KBgaydabura
#34 2731991-34.patch3.88 KBgaydabura
#28 2731991-28.patch3.73 KBGrandmaGlassesRopeMan
#25 2731991-26.patch3.91 KBr.nabiullin
#24 2731991-24.patch3.73 KBGrandmaGlassesRopeMan
#24 interdiff-2731991-21-24.txt4.36 KBGrandmaGlassesRopeMan
#22 drupal-states-required-options-2731991-21.patch2.02 KBarunkumark
#15 post_patch5.png29.44 KBrobpowell
#15 drupal-states-required-options-2731991-5.patch2.09 KBrobpowell
#11 drupal_8.1.8_radio_state.png12.73 KBrobpowell
#5 After-patching.jpg34.58 KBsurbz
#5 Before-patching.jpg35 KBsurbz
#4 drupal-states-required-options-2731991-4.patch800 bytesr-mo
#3 drupal-states-required-options-2731991-3.patch799 bytesr-mo
#2 drupal-states-required-options-2731991-2.patch799 bytesr-mo
states_required_radios.png17.85 KBr-mo
Support from Acquia helps fund testing for Drupal Acquia logo

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.

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

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

andypost’s picture

andypost’s picture

Status: Needs review » Needs work
arunkumark’s picture

Status: Needs work » Needs review
FileSize
2.02 KB

The patch has been re-rolled for Drupal 8.5.x version.

Status: Needs review » Needs work

The last submitted patch, 22: drupal-states-required-options-2731991-21.patch, failed testing. View results

GrandmaGlassesRopeMan’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
4.36 KB
3.73 KB

@arunkumark - Your patch is missing the changes to the ES6 files. See this change record for more information.

- This patch fixes the coding standards errors present in #22
- Includes the changes ported to the .es6.js file.

r.nabiullin’s picture

Rerolled #24 because patch is not applied on D 8.4.3, do not commit this to 8.5.x

Status: Needs review » Needs work

The last submitted patch, 25: 2731991-26.patch, failed testing. View results

r.nabiullin’s picture

Status: Needs work » Needs review
GrandmaGlassesRopeMan’s picture

@nabiyllin Have a look at https://www.drupal.org/core/backport-policy. This change could get committed to 8.5.x and then possibly cherry-picked backwards.

- This is just a reupload of #24.

r.nabiullin’s picture

@drpal it's after patch merging, but I need in this patch now on project which work on 8.4.
I'm sure I'm not the only one who need this.

Version: 8.5.x-dev » 8.6.x-dev

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

andypost’s picture

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
gaydabura’s picture

Status: Needs work » Needs review
FileSize
3.88 KB
7.31 KB

re-rolled

savkaviktor16@gmail.com’s picture

Issue tags: -Needs reroll
volkswagenchick’s picture

Issue tags: +badcamp 2018

tagging for badcamp 2018

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ilya.no’s picture

Meanwhile attaching patch for current core version with update for latest fix in states.es6.js and states.js files.

GrandmaGlassesRopeMan’s picture

volkswagenchick’s picture

Issue tags: +drupalnorth2019

Tagging for DrupalNorth 2019

volkswagenchick’s picture

Issue tags: +dcco2019

Tagging for DrupalCamp Colorado 2019 (Sunday August 4)

audacus’s picture

I use a slightly modified version of #38 for core 8.7.3 with webform 5.0-beta12.
Instead of setting the required classes onto the legend element I have to set the required classes onto the legend .fieldset-legend span element.

jrockowitz’s picture

#2951317: Radios element missing "required" attribute is a related issue and is the CSS work-around from#17.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

volkswagenchick’s picture

Issue tags: -JavaScript +JavaScript, +sfdug2020

Tagging for SFDUG Global contrib weekend event (Thursday Jan 23)

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

piggito’s picture

Re-rolled for 9.1.x

himanshu_sindhwani’s picture

The patch mentioned in #47 doesn't seem to work for me. I created two fields namely test and test condition. I added code to make test condition required when test has value = 'one' using form alter. What I observe before applying the patch is the div wrapper of the test condition radio button gets an attribute of required when the value of test = 'one' and the same is happening after I apply the patch.
Here is the screenshot which will make it more clear, what I did :

code

test

You can clearly see the required attribute on div which is completely useless. It should have been on one of the input fields of radio button.

himanshu_sindhwani’s picture

Status: Needs review » Needs work
himanshu_sindhwani’s picture

Here, I have created a patch to fix the issue faced in #48. This patch will add the required attribute on the input tag instead of the div tag. How ever the tags like texarea are not handled. For them to be required we have add a custom validation on form submit as implemented in body field when you make it required.

himanshu_sindhwani’s picture

Status: Needs work » Needs review
priyanka.sahni’s picture

Assigned: Unassigned » priyanka.sahni
priyanka.sahni’s picture

Assigned: priyanka.sahni » Unassigned
sharma.amitt16’s picture

After applying patch #50, it works for me.

Check the below screenshot, it is working as expected.

After patch apply

Alter hook look like below

$form['field_conditional']['#states']['required'] = [
    ':input[name=field_test]' => ['value' => 'one'],
  ];

Looks good to me for RTBC.

batkor’s picture

Thank guys!
Patch #42 worked for my.
Drupal 8.8.5.

Patch #50 not apply

https://www.drupal.org/files/issues/2020-05-26/2731991-50.patch (issue:#2731991 (Setting required on radios marks all options required))
   Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2020-05-26/2731991-50.patch

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

quietone’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

I thought I would test this but the Issue Summary does not include steps to reproduce. :-( I went ahead and added select list fields to the article content type on a fresh install of 9.2.x and using that I was not able to reproduce the problem.

The issue summary only describes the problem. tagging for issue summary update. For help with that there are Issue Summary Template examples available.

clairedesbois@gmail.com’s picture

Patch #42 works for me in Drupal 8.9.13

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

viniciusrp’s picture

Patch #50 works for me in Drupal 9.2.x

vsujeetkumar’s picture

Status: Needs work » Needs review
FileSize
2.71 KB

Re-roll patch created for 9.3.x.

vsujeetkumar’s picture

Fixed "Custom command fail" issue. Please have a look.

vsujeetkumar’s picture

Please ignore my previous(#62) patch, Fixed custom command fail issue.

Status: Needs review » Needs work

The last submitted patch, 63: 2731991-63.patch, failed testing. View results

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs work » Postponed (maintainer needs more info)

Moving to PNMI as the summary needs an update. I tried replicating locally but was not able to. The required seems to just go to the label which is desired.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

pameeela’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)
FileSize
4.01 KB

Definitely does not occur now, in Claro or Seven:

andresgmh’s picture

FileSize
2.88 KB

Re-rolling to 9.5.7 version

SanderJP’s picture

I would not recommend using this patch. The changes to core/misc/states.js are breaking form states on textfields and textareas, you cannot make those fields required anymore. Took me way too long to figure that out.