1

In my project I have a tickets which are edited in multiple steps where each step has it's own front end for. Now, of course. to continue editing an ticket I am required to fetch that exact ticket.

To accomplish this I have a search form include which will be displayed before the form is showed. When the search form returns an entry the form is shown, when this fails the search form include will be shown again.

Lastly: The search does work when I search for an entry where the title does not contain a spacebar, So:

When the title is 'Test' I can find it, when the title is test ticket I can't find it using the search form, even though they do both exist!

My code:

General.php settings:

'defaultSearchTermOptions' => [
    'attribute' => 'title',
    'exact' => true,
    'subRight' => false,
    'subLeft' => false,
],

My search form:

<form novalidate>
    <input id="search" type="search" name="ticket_number"/>
    <button type="submit">Search</button>
</form>

My URL after submitting the form:

domain.com?ticket_number=ticket+number+test+search

My page:

{% set searchQuery = craft.app.request.getParam('ticket_number') %}
{% set ticket = craft.entries().search(searchQuery).section('ticketLog').one() %}

{% if ticket|length %}
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">Start Picking - {{ ticket.title }}</h4>
                    <h6 class="card-subtitle">Fill in this form to start picking.</h6>
                    {% include '/_components/includes/form' with {
                        'redirect' : siteUrl,
                        'section_handle' : 'ticketLog',
                        'fields' : [{
                            type : 'datetime',
                            handle : 'pickingStartTime',
                        },{
                            type : 'user',
                            title : 'Picker',
                            query : 'teamleader',
                            required : 'required',
                            size : '12',
                        }, {
                            type : 'submit',
                            text : 'Save Ticket',
                        }]
                    } %}
                </div>
            </div>
        </div>
    </div>

{% else %}
    <p>Your search for “{{ searchQuery }}” didn’t return any Tickets, please try again below.</p>
    {% include 'ticket_log/forms/search_form.twig' %}
{% endif %}

Hope someone sees what I am doing wrong there!

1

I think the problem originates from the space in your search term: test ticket. To verify that, could you try altering your query to either

.search('"' ~ searchQuery ~ '"')

Or

.search('fieldHandle:"' ~ searchQuery ~ '"')

The latter can be used if the search has to take place in only one field.

  • That indeed solves the problem. Thank you! Accepted this answer. – mike Mar 7 '19 at 13:25
0

I found out that the problem can be resolved by changing the query from :

{% set ticket = craft.entries().search(searchQuery).section('ticketLog').one() %}

to

{% set ticket = craft.entries().title(searchQuery).section('ticketLog').one() %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.