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'),
),
),
...
Comments
Comment #1
almc commentedComment #2
almc commentedComment #3
almc commentedComment #4
almc commentedComment #5
almc commentedI've tried to correct states.js to avoide multiple asterisks, particularly made:
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.
Comment #6
almc commentedI'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.
Comment #7
longwaveI 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.
Comment #8
almc commented#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.
Comment #9
longwaveBugs that exist in both Drupal 7 and 8 are fixed in 8 first, and then backported.
Comment #10
almc commentedyeah ... 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.
Comment #11
almc commentedComment #12
bogdan khrupa commentedFixed 'required' multiple state.
Comment #13
heddnDuplicate of #1592688: #states can cause the form "required" mark to appear more than once on the same element