Search
When to use this component
Use the search component when you need to let users search and select a record from a database table. e.g. A users might enter a Companies House Number to find an organisation. When submitting data to CRM, the target field should be a lookup to the selected record.
When to not use this component
Do not use the search component as part of a table. In this case, you should use the searchable table component instead.
How it works
Always use the search component as the only component on a page. The search component is often accompanied by a radio component to display the search results, which requires the user to choose an option. If the search and related components are not the only compononents on the page it is more likely that users will not realise they've missed a question.
Set the contents of the <label>
as the page heading. This is good practice as it
means that users of screen readers will only hear the contents once.
You will need to include an empty {% title %}
block in your web template to override the default page header.
Copy the HTML example
Choose the appropriate example from this page and copy the HTML directly into your web template. Read about layouts for more information about how to structure your web template.
If you need to submit the search value to a field in CRM, replace the highlighted search
text in the example
HTML with the target field schema name. Do not remove any prepended or appended text to the schema name. For example, if the id attribute is
displayed as search-error
in the example, the value should still contain -error
after the search text has been replaced (dfe_field-error
).
This step is essential for the custom portal helper framework to validate and submit data to CRM. Read about data validation and submitting data to CRM.
If you do not need to submit the search value to a field in CRM, you can copy the HTML example as it is displayed.
Radio component
<div class="govuk-form-group">
<h1 class="govuk-label-wrapper">
<label class="govuk-label govuk-label--l" for="search">
Enter your URN and click the search icon
</label>
</h1>
<div class="govuk-hint" id="search-hint">
If you don't know your URN, you can find it on the Get information about schools (GIAS) service.
</div>
<p id="search-error" class="govuk-error-message govuk-visually-hidden">
<span class="govuk-visually-hidden">Error:</span>
</p>
<div class="dfe-form-search__item-wrapper">
<input class="dfe-form-search__item dfe-form-search__input govuk-!-width-two-thirds" id="search" name="search" title="Search" type="search">
<div class="dfe-form-search__item dfe-form-search__submit-wrapper">
<button class="dfe-form-search__submit" id="search-btn" type="submit">
Search organisation
<svg class="dfe-form-search__icon" width="27" height="27" viewBox="0 0 27 27" fill="none"
xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false">
<circle cx="12.0161" cy="11.0161" r="8.51613" stroke="currentColor" stroke-width="3"></circle>
<line x1="17.8668" y1="17.3587" x2="26.4475" y2="25.9393" stroke="currentColor" stroke-width="3"></line>
</svg>
</button>
</div>
</div>
</div>
<div id="search-loader" class="loader govuk-visually-hidden"></div>
{% extends 'DfE/Layout/2ColumnWideLeft' %}
{% block title %}{% endblock %}
{% block main %}
## HTML content goes here ##
{% endblock %}
Using hint text
Use hint text for help that's relevant to the majority of users, like how their information will be used, or where to find it. The hint text block is included in the previous examples on this page.
Hint text is not mandatory, and can be removed if it is not required.
Radio component
<div class="govuk-form-group">
<h1 class="govuk-label-wrapper">
<label class="govuk-label govuk-label--l" for="search">
Enter your URN and click the search icon
</label>
</h1>
<p id="search-error" class="govuk-error-message govuk-visually-hidden">
<span class="govuk-visually-hidden">Error:</span>
</p>
<div class="dfe-form-search__item-wrapper">
<input class="dfe-form-search__item dfe-form-search__input govuk-!-width-two-thirds" id="search" name="search" title="Search" type="search">
<div class="dfe-form-search__item dfe-form-search__submit-wrapper">
<button class="dfe-form-search__submit" id="search-btn" type="submit">
Search organisation
<svg class="dfe-form-search__icon" width="27" height="27" viewBox="0 0 27 27" fill="none"
xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false">
<circle cx="12.0161" cy="11.0161" r="8.51613" stroke="currentColor" stroke-width="3"></circle>
<line x1="17.8668" y1="17.3587" x2="26.4475" y2="25.9393" stroke="currentColor" stroke-width="3"></line>
</svg>
</button>
</div>
</div>
</div>
<div id="search-loader" class="loader govuk-visually-hidden"></div>
{% extends 'DfE/Layout/2ColumnWideLeft' %}
{% block title %}{% endblock %}
{% block main %}
## HTML content goes here ##
{% endblock %}
When not to use hint text
Do not use long paragraphs and lists in hint text. Screen readers read out the entire text when users interact with the form element. This could frustrate users if the text is long.
Avoid links in hint text
Do not include links within hint text. While screen readers will read out the link text when describing the field, they will not tell users that the text is a link.
Minimum and maximum length
Use custom minchar
and maxchar
attributes to set a minimum or maximum length for your search input. For example, you may wish to use input minchar="6" maxchar="6"
when asking for a school's URN. This will ensure the user can only submit a search query of 6 characters in length.
You can use the minchar
attribute together with the maxchar
attribute to create a range of allowed lengths, as described in the example above, or as individual attributes.
Use appropriately-sized inputs
Help users understand what they should enter by making search inputs the right size for the content they're intended for.
By default, the examples on this page contain the input class govuk-!-width-two-thirds
. Removing this class makes the width of search inputs fluid and they will fit the full width of the container they are placed into.
If you want to change the search input size, you can either use a fixed width input, or use the width override classes.
Read more about sizing your inputs and see examples (opens in a new tab) on the GOV.UK Design System.
Display your search results
Your search results should be displayed in an accompanying radio component unless your user research shows there is need for an alternative approach.
Read about how to display search results using a radio component.
Error messages
An error message is shown next to the field and in the error summary when there is a validation error. The HTML content for these errors is already included in the component examples and in the base portal web template set up, therefore you do not need to add any additional error containers to your page.
The portal helper framework validation process automatically handles error messages for specific error states.
If nothing is entered
'Enter [whatever it is]'.
For example, 'Enter your URN'.
If the input is too short
'[whatever it is] must be [number] characters or more'.
For example, 'Your reference number must be 8 characters or more'.
If the input is too long
'[whatever it is] must be [number] characters or less'.
For example, 'Your reference must be 8 characters or less'.
If the input has both a minimum and maximum length
'[whatever it is] must be between [number] and [number] characters'.
For example, 'Your reference number must be between 6 and 8 characters'.
If the input has equal value minimum and maximum lengths
'[whatever it is] must be [number] characters'.
For example, 'Your URN must be 6 characters'.
Occasionally you may wish to use error messages outside of the validation process. You can read about the functions available within the portal helper framework to help you with this.