Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x]: Query with caseInsensitive: true not working when plain text field contains a comma #16409

Open
vonscriptor opened this issue Jan 10, 2025 · 3 comments
Labels

Comments

@vonscriptor
Copy link

What happened?

Description

References

  1. caseInsensitive feature added: Case insensitive custom field params #15404
  2. Documentation: https://craftcms.com/docs/5.x/development/element-queries.html#case-sensitivity

I have a Plain Text field which has values that include a comma(s) ,. When I run a query against that field I add caseInsensitive: true but it doesn't work due to a comma(s) contained in the field value.

Steps to reproduce

  1. Create a Plain Text field and add it to an entry/user.
  2. Give that entry's/user's field a value that includes a comma (e.g. "example,").
  3. Run an entry/user query using with that plain text field that has the value and caseInsensitive: true.
  4. Test querying using different cases for the value (e.g. "Example", "example") and notice how caseInsensitive has no effect—only matching if its case matches.
  5. Remove the comma from the entry's/user's field value and caseInsensitive should then work as expected.

Expected behavior

A comma(s) in the value of a Plain Text field should not cause caseInsensitive: true to not be applied.

Actual behavior

A comma(s) in the value of a Plain Text field causes caseInsensitive: true to not be applied.

Craft CMS version

5.5.9 Pro

PHP version

8.2

Operating system and version

macOS 15.2

Database type and version

MySQL 8.3.0

Image driver and version

Imagick 3.7.0

Installed plugins and versions

  • CKEditor 4.4.0
  • Freeform 5.8.6

(and more…)

@i-just
Copy link
Contributor

i-just commented Jan 13, 2025

Hi, thanks for reaching out!

That’s not related to the caseInsensitive feature but to querying elements with field values with commas in general. Comma and asterisk characters are not treated literally unless you say that they should. To do so, you can use the literal filter.

For example:

{% set entries = craft.entries()
    .section('blog')
    .plainText({
        value: 'tes,t'|literal,
        caseInsensitive: true,
    })
    .all() %}

We’re updating the docs with this info!

@i-just i-just closed this as completed Jan 13, 2025
@vonscriptor
Copy link
Author

Thanks for the info and example. I'm still having troubles getting this working. Here's what I'm doing…

My plainText field will have varying values such as:

  1. "React, CSS, Typescript"
  2. "html, css, typescript"

I'm using a form with a select (multiple) of predefined values such as:

  • React
  • CSS
  • TypeScript

The select seems to send them as an array (e.g. skills[]: typescript).

I'm adding in * around the select option values for wildcard/partial matches. Adding |literal to these is not working as expected—it still isn't using caseInsensitive true and therefore returning fewer or no results when I expect there to be some. Note, I'm also searching users for my use case rather than entries.

Using your example how can it work with wildcard/partial match * support too?

@i-just
Copy link
Contributor

i-just commented Jan 17, 2025

Right, the extra info you just provided helps - thanks!

Let me check if I understand what you’re trying to do correctly:

  • you have a section with entries that have a Plain Text field in them (let’s assume the handle of that field is skills)
  • for one of the entries, the content of the skills is set to React, CSS, Typescript; let’s call it “entry one”
  • for another entry, the content of the skills is set to html, css, typescript; let’s call it “entry two”
  • in your front end, you have a search form which allows the user to select multiple skills
  • you want to return all entries from that section that contain any of the selected skills; so if someone selects just css, “entry one” and “entry two” should be returned; if someone selects html and react, also both entries should be returned; if someone selects react just “entry one” should be returned;

The code you’re using is something like this:

{% set entries = craft.entries()
    .section('<your_section_handle')
    .skills({
    	value: '*css*, *typescript*',
    	caseInsensitive: true
	})
    .all() %}

and it should return both entries, but it only returns one with a matching case, right?
I think that might be a bug. I’ll have a look into it.

Side note:
You could use .search() in this case too. To do that, first, ensure that the skills field has “Use this field’s values as search keywords” lightswitch turned on (if it doesn’t, you want to switch it on and then run php craft resave/entries --section=<your_section_handle> --update-search-index=1 to ensure search index is up to date) and then you could use:

{% set entries = craft.entries()
	.section('<your_section_handle>')
	.search('skills:html OR skills:react')
	.all() %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants