Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Tried to use this code piece in an install profile I'm building:
$form['l10n_client_wrapper']['l10n_share_wrapper']['l10n_client_key'] = array(
'#type' => 'textfield',
'#title' => st('Your personal key on localize.drupal.org'),
'#description' => st('Each user account on the site should have their own localize.drupal.org key set up so the remote submission can be attributed properly. To get your key go to !server-link.', array('!server-link' => l($server_link, $server_link))),
'#states' => array(
'visible' => array(
'input[name="l10n_client_sharing"]' => array('checked' => TRUE),
),
'required' => array(
'input[name="l10n_client_sharing"]' => array('checked' => TRUE),
)
),
);
Visible works like a charm. However, required adds the form-required class on the form label itself, making the whole form label red all over. In practice Drupal just uses this class on a span which has * in it. So I think this should in fact add a star with that class and remove a star with that respectively instead of this code that we have now there.
$(document).bind('state:required', function(e) {
if (e.trigger) {
$(e.target).closest('.form-item, .form-submit, .form-wrapper')[e.value ? 'addClass' : 'removeClass']('form-required');
}
});
Comment | File | Size | Author |
---|---|---|---|
#21 | drupal-required-states-first-child-1017882-21.patch | 816 bytes | deetergp |
#15 | drupal-required-states-first-child-1017882-15.patch | 1.21 KB | alexverb |
#14 | multiple-asterixes.jpg | 209.91 KB | alexverb |
#3 | states-required.patch | 818 bytes | Gábor Hojtsy |
#2 | states-required.patch | 815 bytes | Gábor Hojtsy |
Comments
Comment #1
Gábor HojtsyUps, in fact it puts the class on the .form-item, so all the children got to be red. The only thing stopping them is that the text input field and then the description of the form item all have specific colors in the CSS. If they would not have, all would be red.
Comment #2
Gábor HojtsySuggested patch adds a star with the right class and removes it respectively. Although I don't think this makes any sense for submit buttons, so removed that item.
Comment #3
Gábor HojtsyUps, tag mismatch with the span.
Comment #4
effulgentsia CreditAttribution: effulgentsia commentedsubscribe
Comment #5
pwolanin CreditAttribution: pwolanin commentedLooks good and works.
Comment #6
james.elliott CreditAttribution: james.elliott commentedThis also brings the state change inline with how a required field is themed.
Comment #7
joelstein CreditAttribution: joelstein commentedThe patch in #3 looks and works great for me.
Comment #8
ikpoe CreditAttribution: ikpoe commentedpatch from #3: The theme with asterisk is working fine, but the javascript validation is not working. There is no error warning if I leave the input (I tested using textfield) blank. It should be an error warning if I leave the input blank, because it is required field.
Comment #9
Gábor HojtsyNo, it is not going to be required on the server side, and we should be consistent with the validation. This is just an indication that we'd like the user to fill it in. If it is an issue that it is not validated as required, we should remove this feature entirely as it does not make sense. Looking for committer feedback.
Comment #10
dawehnerComment #11
Dries CreditAttribution: Dries commentedCommitted to 7.x and 8.x. Thanks!
Comment #12
aspilicious CreditAttribution: aspilicious commenteduntagging
Comment #14
alexverb CreditAttribution: alexverb commentedOn a multiple checkboxes element it puts an asterix after the main label and each checkbox label. Is this the wanted behaviour? I would think it should only append on the main label.
See attachement.
Comment #15
alexverb CreditAttribution: alexverb commentedAppending the asterix to the first-child only seems to fix it. See patch
Comment #16
alexverb CreditAttribution: alexverb commentedForgot to update status.
Comment #17
mstrelan CreditAttribution: mstrelan commented#15 works for me ... not sure if there are any other associated issues with it. We should probably compare this to the logic used when PHP adds the required asterisk.
Comment #18
almc CreditAttribution: almc commentedThe issue remains with D7.23. For 'required' state when it's activated 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 currently 'required' state doesn't work.
Comment #19
almc CreditAttribution: almc commentedAlso, the patch #15 doesn't apply in D7.23: 'The patch cannot be applied in the selected context'
Comment #20
deetergp CreditAttribution: deetergp commentedI am seeing that same behavior in 7.27.
Comment #21
deetergp CreditAttribution: deetergp commentedI rerolled the patch from #15 so that it applies in Drupal 7.27. This patch only addresses the "*" appearing on the first child. Does not resolve the issue where the items in question are not actually required, in spite of being flagged as such.
Comment #23
deetergp CreditAttribution: deetergp commentedOkay, taking a step back, it seems as though this is a Drupal 8 ticket. Looking at the latest version of the current /core/misc/states.js file, it looks like it has been completely revamped and looks nothing like any of the patches posted thus far. At this point, I'm not quite sure what is the right way to go about addressing this in Drupal 7. Any thoughts on the matter would be appreciated.
Comment #24
deetergp CreditAttribution: deetergp commentedI changed the version on this ticket back to 7.x-dev. Drupal 8 deals with this issue in different manner that isn't easily back-ported. Since the patch being passed around pertains to D7, it makes sense for the ticket to also be D7.
Comment #25
deetergp CreditAttribution: deetergp commented21: drupal-required-states-first-child-1017882-21.patch queued for re-testing.
Comment #26
lmeurs CreditAttribution: lmeurs commentedPatch from #1017882-21: Required elements buggy with #states (manually applied) works as advertised, though as deetergp states: only an element with the asterisk is added, the required attribute is not being toggled on the input element. This seems not so hard to implement, but I worry about nested form fields like ie. the managed file and AdressField fields which exist out of multiple child form fields.
Marked #2298809: Compound form element #states property and required mark as duplicate.
Comment #27
juanjo_vlc CreditAttribution: juanjo_vlc commented#21 works for me. Thanks!
Comment #28
heddnThis is moderately related to #1592688: #states can cause the form "required" mark to appear more than once on the same element, which resolves the issue with duplicate asterisks getting added on subsequent ajax calls on the page.
The underlying issue in the issue summary isn't really addressed in any of the recent patches, so leaving this at needs work.
Comment #29
dtamajon CreditAttribution: dtamajon commentedHi,
I have applied the following patch where is solved the 'required' state and duplicity problems, producing the same code as per default:
Comment #30
kenorb CreditAttribution: kenorb commentedAfter #1592688: #states can cause the form "required" mark to appear more than once on the same element fix, is it still a problem?
Comment #31
dtamajon CreditAttribution: dtamajon commentedFrom my point of view, it "looks like" the problem is solve, but the behavior it not being exactly the same than when the field is sent from the server. I mean, for example, a required field has a tooltip indicating it is "This field is required.", and has some additional attributes.
On the given solution, javascript is not reproducing exactly the same behavior than the field generated from the form.inc. In #29, I tried to reproduce that behavior exactly, so the feeling is the same from whatever the field gets required.
Comment #32
Lukas von Blarer[ #1592688] solved this issue for me as well. marking this as a duplicate.