Skip to content

Commit 6a4b52a

Browse files
committed
Feat: Custom error messages for Address lookup element.
Adding custom error messages to several subelements of the Address lookup element. This is for better UX.
1 parent b24fb97 commit 6a4b52a

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

src/Element/AddressLookupElement.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public static function processAddressLookupElement(&$element, FormStateInterface
101101
'#type' => 'textfield',
102102
'#title' => t('Postcode or street'),
103103
'#description' => $element['#address_search_description'] ?? t('Enter the postcode…'),
104+
'#required_error' => t('You must enter a postcode or street.'),
104105
'#maxlength' => 64,
105106
'#size' => 64,
106107
'#weight' => '0',

src/Element/UKAddressLookup.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public static function getCompositeElements(array $element) {
5959
$element_list['address_1']['#prefix'] = '<div class="js-address-entry-container">';
6060
$element_list['postcode']['#suffix'] = '</div>';
6161

62+
// Custom error message for address line 1.
63+
$element_list['address_1']['#required_error'] = t('You must select an address.');
64+
6265
// Extras to store information for webform builders to access in
6366
// computed twig.
6467
// @See DRUP-1287.
@@ -161,7 +164,7 @@ public static function validateWebformComposite(&$element, FormStateInterface $f
161164
// Then show an error to search for a local address or select can't find
162165
// the address.
163166
if (!empty($search_string) && $element['address_lookup']['address_select']['address_select_list']['#type'] == 'markup') {
164-
$form_state->setError($element['address_lookup']['address_search']['address_searchstring'], t('Search for a local address, or select "Can\'t find the address" to enter an address.'));
167+
$form_state->setError($element['address_lookup']['address_search']['address_searchstring'], t('Enter a local address, or select "Can\'t find the address".'));
165168

166169
// Inline form errors don't work well for this element in Ajax calls.
167170
// This is because the Ajax callback attached to the `Find address`

src/Element/WebformUKAddress.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public static function getCompositeElements(array $element) {
6565
$elements['town_city'] = [
6666
'#type' => 'textfield',
6767
'#title' => t('Town/City'),
68+
'#required_error' => t('You must enter the town/city.'),
6869
'#attributes' => [
6970
'data-webform-composite-id' => $html_id . '--town_city',
7071
// Add a namespaced class for setting the address fields
@@ -78,6 +79,7 @@ public static function getCompositeElements(array $element) {
7879
$elements['postcode'] = [
7980
'#type' => 'textfield',
8081
'#title' => t('Postcode'),
82+
'#required_error' => t('You must enter the postcode.'),
8183
'#attributes' => [
8284
'data-webform-composite-id' => $html_id . '--postcode',
8385
// Add a namespaced class for setting the address fields

0 commit comments

Comments
 (0)