Readonly doesn't work on #States

Created on 3 April 2017, over 7 years ago
Updated 3 April 2023, over 1 year ago

Problem/Motivation

When I use the #state structure below to turn a text field into read-only depending to a checkbox selection, simply nothing happens.

    'states' => array(
      'readonly' => array(
        ':input[name="text_field"]' => array('checked' => TRUE),
      ),
    ),

If I use 'disabled' instead of 'readonly' the code works perfectly! But I really need the "readonly" option because I want to save the value filled in the text field before. The "disabled" option doesn't allow to save the field value.

Proposed resolution

I found a simply solution that solves the problem with few lines: just implement the "readonly" option in the file states.js, the same way it is done with the "disabled" option. This way, readyonly works very well.

๐Ÿ› Bug report
Status

Fixed

Version

10.0 โœจ

Component
Javascriptย  โ†’

Last updated about 2 hours ago

  • Maintained by
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom @justafish
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance @nod_
Created by

๐Ÿ‡ง๐Ÿ‡ทBrazil luizps

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request โ†’ as a guide.

    Did not test but still needs tests.

  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Abhisheksingh27

    Added Reroll for 10.1.x as #26 patch failed to apply on 10.1.x.
    Removed changes of "a/core/misc/states.es6.js" as file doesn't exist.

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Thank you but please read the comments and tags before changing status. Still needs tests

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Nitin shrivastava

    fix command failures

  • Status changed to Needs review over 1 year ago
  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Thank you for working on this but please see the tags also. This needs tests so this isn't ready for review.

  • First commit to issue fork.
  • @sunlix opened merge request.
  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany sunlix Wesel

    Hey,

    I have added state:readonly with running Javascript Tests as well.
    I hope that is properly implemented for a valid core commit. :)

  • Status changed to RTBC over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Test assertions added cover the change being made.

    Ran locally without the fix and got failing assertions

    Failed asserting that false is true.

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance nod_ Lille

    Really wanted to commit that but readonly doesn't work on checkboxes, the test need to use an input type that supports the readonly attribute.

    The attribute is added correctly and we can see that in the test, it's just that it's not supposed to do anything on radio/checkboxes/color/file/select

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany sunlix Wesel

    @nod_
    Thank you for your feedback. I have done a little bit research on the spec and MDN.
    Is far as I understand we have to limit the relvant input options to text, search, url, tel, email, password, date, month, week, time, datetime-local, and number or a negated version of checkbox, radio, file, range, color, hidden for the input types.

    And for sure adjust the test suite properly. Am I right?

    From MDN

    The readonly attribute is supported by text, search, url, tel, email, password, date, month, week, time, datetime-local, and number types and the form control elements. If present on any of these input types and elements, the :read-only pseudo class will match. If the attribute is not included, the :read-write pseudo class will match.

    The attribute is not supported or relevant to
    or input types that are already not mutable, such as checkbox and radio or cannot, by definition, start with a value, such as the file input type. range and color, as both have default values. It is also not supported on hidden as it can not be expected that a user to fill out a form that is hidden. Nor is it supported on any of the button types, including image.

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance nod_ Lille

    I don't think we need to be too precise, if someone uses it on an input that doesn't support readonly it won't break anything specifically.

  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance nod_ Lille

    great, thanks :)

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany sunlix Wesel

    @_nod

    wow, you are great :) Thank you for the proactive obversation.
    I just wanted to wait for the tests are green :D I think I got it. So thank you! :)

    Just want one follow up question.
    The visual representation of the state is nothing special by user agent style.
    Should claro & olivero represent the readonly state equal to the disabled state on theme level?

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance nod_ Lille

    We could open a new issue to address the styling of readonly fields. The styling is out of scope for this issue though.

  • Issue was unassigned.
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance nod_ Lille
  • Status changed to RTBC over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States dww

    The tests are right where they need to be, the additions are clean and follow the existing patterns in the test, are passing now (and as reported by smustgrave, failing when run without the fix).

    The fix looks clean and good.

    I donโ€™t see any problems with the changes in the MR.

    RTBC

    thanks!
    -Derek

    • nod_ โ†’ committed ac64dce7 on 10.1.x
      Issue #2866383 by sunlix, SAVEL, Nitin shrivastava, narendra.rajwar27,...
  • Status changed to Fixed over 1 year ago
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance nod_ Lille

    Committed ac64dce and pushed to 10.1.x. Thanks!

    • nod_ โ†’ committed 8dcb9036 on 10.0.x
      Issue #2866383 by sunlix, SAVEL, Nitin shrivastava, narendra.rajwar27,...
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance nod_ Lille

    Checked in with lauriii, If we consider this a bug (as in: it's expected to be available based on what is possible inside HTML forms), backporting to 10.0.x also because it's better to keep the states API in sync when possible.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany luenemann Sรผdbaden, Germany
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024