Instructions for using with Form API cause required validation to be skipped

Created on 31 October 2024, 24 days ago

Problem/Motivation

If following the instructions in the README.md for using the country field via Drupal Form API, the #required setting does not work. This is because the '#empty_value' is set the 'none'.

$form['country'] = [
  '#type' => 'country',
  '#title' => $this->t('My country'),
  '#required' => TRUE,
  '#multiple' => TRUE,
  '#empty_option' => 'Country',
  '#empty_value' => 'none',
  '#default_value' => ['BE', 'NL', 'LU']
];

Steps to reproduce

  1. Create a custom form using Form API
  2. Add the field using the suggested code from the README.md file
  3. Try submit the form and it doesn't validate as being empty

Proposed resolution

Remove the #empty_value setting from the README.md or set it to ''.

📌 Task
Status

Active

Version

2.0

Component

Documentation

Created by

🇬🇧United Kingdom joehuggans Harrogate, UK

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024