Skip to content

Commit

Permalink
Fixes date field validation message presents in reverse order (#72)
Browse files Browse the repository at this point in the history
* Added UK short date format

* Amended unit webform element test to load deault configuraion

* Amended webform element unit test to load default localgov forms date configuration

* Applied coding standard fixes

* Amended the date formats namespace to include localgov_forms

* Corrected namespace

* Removed html from the name

---------

Co-authored-by: Matt Ozogolu <2003150@croydongov.uk>
  • Loading branch information
MattOz-CDS and Matt Ozogolu committed Apr 23, 2024
1 parent b9d8ce4 commit e092414
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
langcode: en
status: true
dependencies: { }
id: localgov_forms_date_datetime
label: 'LocalGOV Forms Datetime'
locked: false
pattern: 'd-m-Y\TH:i:sO'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
langcode: en
status: true
dependencies: { }
id: localgov_forms_date_short_date
label: 'LocalGOV Forms Short Date'
locked: false
pattern: d-m-Y
28 changes: 28 additions & 0 deletions modules/localgov_forms_date/localgov_forms_date.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* @file
* Install, update and uninstall functions for the LocalGov Forms Date module.
*/

use Drupal\Component\Serialization\Yaml;

/**
* Adds a UK short date style e.g dd-mm-yyyy.
*/
function localgov_forms_date_update_8001() {
$config_id = 'core.date_format.localgov_forms_date_short_date';
$config_path = \Drupal::service('extension.list.module')->getPath('localgov_forms_date');
$uk_short_html_date_config = Yaml::decode(file_get_contents($config_path . '/config/install/' . $config_id . '.yml'));
\Drupal::configFactory()->getEditable($config_id)->setData($uk_short_html_date_config)->save();
}

/**
* Adds a UK short date style e.g dd-mm-yyyy HH:mm:ss.
*/
function localgov_forms_date_update_8002() {
$config_id = 'core.date_format.localgov_forms_date_datetime';
$config_path = \Drupal::service('extension.list.module')->getPath('localgov_forms_date');
$uk_html_datetime_config = Yaml::decode(file_get_contents($config_path . '/config/install/' . $config_id . '.yml'));
\Drupal::configFactory()->getEditable($config_id)->setData($uk_html_datetime_config)->save();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\localgov_forms_date\Plugin\WebformElement;

use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformElement\DateList;

Expand Down Expand Up @@ -119,4 +120,23 @@ public function setDefaultValue(array &$element) {
$element['#type'] = $orig_type;
}

/**
* {@inheritDoc}
*
* Overrides the Datebase class so that
* date validation error messages display in
* a UK Style format e.g dd-mm-yyyy.
*/
public static function validateDate(&$element, FormStateInterface $form_state, &$complete_form) {

// Adds a short date and short date time format.
$localgov_forms_short_date_format = DateFormat::load('localgov_forms_date_short_date')->getPattern();
$localgov_forms_datetime_format = DateFormat::load('localgov_forms_date_datetime')->getPattern();

$element['#date_date_format'] = $localgov_forms_short_date_format;
$element['#date_time_format'] = $localgov_forms_datetime_format;

parent::validateDate($element, $form_state, $complete_form);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ protected function setUp() :void {
$this->installConfig('system');
$this->installConfig('localgov_forms_date_test');

$this->installConfig('localgov_forms_date');

$empty_submission = WebformSubmission::create(['webform_id' => 'date_test']);
$this->testForm = WebformSubmissionForm::create($this->container);
$this->testForm->setEntityTypeManager($this->container->get('entity_type.manager'));
Expand Down

0 comments on commit e092414

Please sign in to comment.