Autocomplete field with "STARTS_WITH" enabled

Created on 9 September 2024, 7 months ago

Problem/Motivation

Hello, I used an autocomplete field on my webform, with the option "starts_with".
This option does not work because of a bad condition in the ajax controller.

Steps to reproduce

Create a webform with autocomplete field, with options "test" and "secondtest" and write "tes" in the field. You will have the 2 options.

Proposed resolution

I will write a patch that fixes the problem.

πŸ› Bug report
Status

Needs work

Version

6.2

Component

Code

Created by

πŸ‡«πŸ‡·France Quentin Massez

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

Merge Requests

Comments & Activities

  • Issue created by @Quentin Massez
  • Issue was unassigned.
  • Status changed to Needs review 7 months ago
  • Status changed to Needs work 7 months ago
  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦

    Thanks for the patch. Please make a merge request and indent with spaces. Can you add a test that would have caught this?

  • First commit to issue fork.
  • πŸ‡§πŸ‡·Brazil PabloNicolas

    Hello there!

    I tested the patch and can confirm it is working as expected! The merge request uses spaces rather than TAB to indent, I might need more time to work on the tests, but I hope to commit something by tomorrow.

    Thank you!

  • Pipeline finished with Success
    7 months ago
    Total: 1934s
    #283331
  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦

    It doesn't look to me like this patch changes anything. It just divides up the conditional into two if statements.

  • πŸ‡§πŸ‡·Brazil PabloNicolas

    At first glance, I had the same thought as you, but the difference when we split it into a nested if is that the logic does not fall on the elseif block. This block just validates the query is not FALSE and uses the CONTAINS operator.
    elseif (stripos($label, $q) !== FALSE)
    Another suggestion would be validating that the operator is empty or equals CONTAINS. Both solutions achieve the same result.

  • Pipeline finished with Success
    7 months ago
    Total: 1800s
    #283629
  • Pipeline finished with Success
    7 months ago
    Total: 1601s
    #283715
  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦

    Yes, I see. It would probably be better to use a switch statement for the value of $operator and then if statements within that.

  • Pipeline finished with Success
    7 months ago
    Total: 1682s
    #283774
  • πŸ‡§πŸ‡·Brazil PabloNicolas

    Cool, just using the switch statement was not enough, but putting a break; on STARTS_WITH case does the trick.

    About testing, I'm not very used to writing unit tests, following the existing ones I added a few lines and it seemed correct, but I could not test it using PHPUnit using ddev :/

    If someone could shed light on how to do the tests properly I would give it a shot.

    Thank you!

  • Pipeline finished with Failed
    6 months ago
    Total: 51s
    #321609
  • Pipeline finished with Failed
    6 months ago
    Total: 226s
    #321624
  • πŸ‡§πŸ‡·Brazil PabloNicolas

    Hello Liam.

    It took me a little while to figure out how to run the tests on my local environments and IDE, but now we have tests for this specific operator.

    Do you mind having a look?

    PS: For those having the same difficulties I had with tests, here is an excellent article on how to integrate Unit tests with DDEV and PHP Storm: https://drupalize.me/blog/debug-any-drupals-phpunit-tests-phpstorm-ddev-...

  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦

    Tests are not passing.

  • Pipeline finished with Failed
    6 months ago
    Total: 7643s
    #323202
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    Using a switch/case could make the code cleaner and prevent the issue.

    
          switch ($operator) {
            case 'STARTS_WITH':
              if (str_starts_with($label, $q)) {
                $matches[$label] = [
                  'value' => $label,
                  'label' => $label,
                ];
              }
              break;
    
            case 'CONTAINS':
            default:
              if (str_contains($label, $q)) {
                $matches[$label] = [
                  'value' => $label,
                  'label' => $label,
                ];
              }
              break;
          }
        }
    
    
  • Pipeline finished with Failed
    5 months ago
    Total: 2011s
    #335105
  • Pipeline finished with Canceled
    5 months ago
    Total: 1250s
    #335588
  • Pipeline finished with Canceled
    5 months ago
    Total: 528s
    #335609
  • πŸ‡§πŸ‡·Brazil PabloNicolas

    The tests are passing now, also fixed some deprecated code and PHPCS warnings.

  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦

    Thanks for the patch. Please move the changes regarding ::willReturnCallback() to a separate issue and merge request.

  • Pipeline finished with Failed
    5 months ago
    Total: 7099s
    #335617
  • πŸ‡§πŸ‡·Brazil PabloNicolas

    pablonicolas β†’ changed the visibility of the branch 3473045-fix-starts-with-autocompletion to hidden.

  • πŸ‡§πŸ‡·Brazil PabloNicolas

    pablonicolas β†’ changed the visibility of the branch 3473045-autocomplete-field-with to hidden.

  • Pipeline finished with Canceled
    5 months ago
    Total: 1623s
    #336657
  • Pipeline finished with Failed
    5 months ago
    Total: 155s
    #336673
  • Pipeline finished with Failed
    5 months ago
    Total: 1075s
    #336676
  • πŸ‡§πŸ‡·Brazil PabloNicolas

    Done @liam morland. The merge request now only contains commits linked to this specific issue. I will create a new issue soon to fix the PHPCS and PHPCBF warnings.

    Thank you!

  • Pipeline finished with Failed
    5 months ago
    Total: 765s
    #336690
  • Pipeline finished with Failed
    5 months ago
    Total: 1906s
    #336701
  • Pipeline finished with Failed
    5 months ago
    #341418
  • Pipeline finished with Failed
    5 months ago
    Total: 1963s
    #341424
  • Pipeline finished with Success
    5 months ago
    Total: 510s
    #350310
  • Pipeline finished with Skipped
    5 months ago
    #351285
  • Pipeline finished with Skipped
    5 months ago
    #351286
  • Status changed to Fixed 5 months ago
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    Looks good.

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Pipeline finished with Failed
    3 months ago
    Total: 848s
    #389933
  • Pipeline finished with Failed
    3 months ago
    Total: 743s
    #392314
  • Pipeline finished with Failed
    3 months ago
    Total: 777s
    #394704
  • Pipeline finished with Failed
    3 months ago
    Total: 488s
    #397054
  • Pipeline finished with Failed
    3 months ago
    Total: 870s
    #397059
  • Pipeline finished with Failed
    3 months ago
    Total: 920s
    #397139
  • Pipeline finished with Failed
    3 months ago
    Total: 739s
    #398090
  • Pipeline finished with Failed
    3 months ago
    Total: 986s
    #398784
  • Pipeline finished with Failed
    3 months ago
    Total: 1007s
    #398827
  • Pipeline finished with Failed
    3 months ago
    Total: 1096s
    #398900
  • Pipeline finished with Success
    3 months ago
    Total: 1073s
    #400507
  • Pipeline finished with Failed
    3 months ago
    Total: 1046s
    #403895
  • Pipeline finished with Failed
    3 months ago
    Total: 842s
    #403997
  • Pipeline finished with Failed
    3 months ago
    Total: 837s
    #404039
  • Pipeline finished with Failed
    3 months ago
    Total: 867s
    #404386
  • Pipeline finished with Failed
    3 months ago
    Total: 960s
    #404393
  • Pipeline finished with Failed
    3 months ago
    Total: 952s
    #404402
  • Pipeline finished with Failed
    3 months ago
    Total: 656s
    #404436
  • Pipeline finished with Failed
    3 months ago
    Total: 986s
    #404446
  • Pipeline finished with Failed
    3 months ago
    Total: 1007s
    #404820
  • Pipeline finished with Success
    3 months ago
    Total: 898s
    #404843
  • Pipeline finished with Failed
    3 months ago
    Total: 262s
    #404971
  • Pipeline finished with Failed
    3 months ago
    Total: 940s
    #404973
  • Pipeline finished with Failed
    3 months ago
    Total: 875s
    #408347
  • Pipeline finished with Failed
    3 months ago
    Total: 881s
    #408445
  • Pipeline finished with Success
    3 months ago
    Total: 794s
    #409455
  • Pipeline finished with Failed
    2 months ago
    Total: 867s
    #409592
  • Pipeline finished with Failed
    2 months ago
    Total: 974s
    #409788
  • Pipeline finished with Failed
    2 months ago
    Total: 1013s
    #409810
  • Pipeline finished with Failed
    2 months ago
    Total: 1029s
    #409840
  • Pipeline finished with Canceled
    2 months ago
    Total: 568s
    #409847
  • Pipeline finished with Failed
    2 months ago
    Total: 1023s
    #409851
  • Pipeline finished with Failed
    2 months ago
    Total: 1081s
    #409870
  • Pipeline finished with Failed
    2 months ago
    Total: 1125s
    #410306
  • Pipeline finished with Failed
    2 months ago
    Total: 1128s
    #410333
  • Pipeline finished with Failed
    2 months ago
    Total: 1082s
    #410733
  • Pipeline finished with Failed
    2 months ago
    Total: 983s
    #413962
  • Pipeline finished with Failed
    2 months ago
    Total: 461s
    #414020
  • Pipeline finished with Failed
    2 months ago
    Total: 1270s
    #414029
  • Pipeline finished with Failed
    2 months ago
    Total: 1006s
    #414057
  • Pipeline finished with Success
    2 months ago
    Total: 966s
    #414073
  • Pipeline finished with Success
    2 months ago
    Total: 941s
    #414631
  • Pipeline finished with Failed
    2 months ago
    Total: 1219s
    #417880
  • Pipeline finished with Success
    2 months ago
    Total: 1219s
    #417936
  • Pipeline finished with Success
    2 months ago
    Total: 1806s
    #417963
  • Pipeline finished with Failed
    2 months ago
    Total: 325s
    #419910
  • Pipeline finished with Failed
    2 months ago
    Total: 3844s
    #419918
  • Pipeline finished with Canceled
    2 months ago
    Total: 822s
    #420071
  • Pipeline finished with Failed
    2 months ago
    Total: 1268s
    #420084
  • Pipeline finished with Success
    2 months ago
    Total: 1169s
    #420122
  • Pipeline finished with Failed
    about 2 months ago
    Total: 726s
    #433501
  • Pipeline finished with Failed
    about 2 months ago
    Total: 270s
    #433517
  • Pipeline finished with Success
    about 2 months ago
    Total: 807s
    #433522
  • Pipeline finished with Success
    13 days ago
    Total: 469s
    #462366
Production build 0.71.5 2024