For 'required' state when it's triggered on the form for a dependent field, multiple red asterisks appear for that required field (particularly if there are other fields on the form with unlimited cardinality and Add new item was clicked for any of them), and 'required' state is not being enforced actually for the dependent field during the form processing. So it seems like currently 'required' state doesn't work.
This is the code used for a text form element dependent (conditionally 'required') on another select element in the form:

...
    '#states' => array(
      'visible' => array(
        ':input[name="master_element"]' => array('value' => 'triggering_select_value'),
      ),
      'required' => array(
        ':input[name="master_element"]' => array('value' => 'triggering_select_value'),
      ),        
    ),        
...
CommentFileSizeAuthor
#12 2125563-12.patch799 bytesbogdan khrupa
#4 snapshot.JPG12.32 KBalmc

Comments

almc’s picture

Issue summary: View changes
almc’s picture

Issue summary: View changes
almc’s picture

Issue summary: View changes
almc’s picture

StatusFileSize
new12.32 KB
almc’s picture

Priority: Normal » Major

I've tried to correct states.js to avoide multiple asterisks, particularly made:

    if (e.value) {
      if (!$(e.target).hasClass('required')) {
        $(e.target).addClass('required');
        $(e.target).closest('.form-item').find('label').append('<span class="form-required">*</span>');
      }
    }

It resolves the issue with duplicate asterisks, however 'required' state still doesn't get enforced through the standard back-end validation, and this may be a deeper issue that Drupal core validation of form seems to be ignoring 'required' class dynamically added to a form element.
Not sure then how such conditional 'required' state is supposed to work in the current Drupal functionality.

almc’s picture

I'm surprised this Drupal core code issue survived through the release 7.26 already ... I still have to patch states.js manually as mentioned above.

longwave’s picture

I think the multiple asterisks is a duplicate of #1017882: Required elements buggy with #states

There is no way that the server side Drupal code can know that a class was added on the client side. #states can only hide or show the element on the client side, it is up to you to perform validation on the server side for conditionally required fields.

almc’s picture

#1017882 seems to focus on the fix for Drupal 8, while Drupal 7.x remains unpatched, at least for multiple asterisks in this issue, fixable in states.js.

longwave’s picture

Bugs that exist in both Drupal 7 and 8 are fixed in 8 first, and then backported.

almc’s picture

yeah ... it just feels like D7 is sometimes lacking attention with D8 holding the spotlight, while no one currently uses D8 for production I guess ... I understand that this backporting approach is primary for Drupal, it's just the lag seems to be quite substantial, last messages in that thread for D8 were dated November last year with the issue reported as fixed for D8.

almc’s picture

Version: 7.23 » 7.26
bogdan khrupa’s picture

Status: Active » Needs review
StatusFileSize
new799 bytes

Fixed 'required' multiple state.

heddn’s picture