From 021e5af709601bf4d7d4370cf9544e3c103b7001 Mon Sep 17 00:00:00 2001
From: Melissa Miller <miller.2676@osu.edu>
Date: Tue, 18 Dec 2018 14:59:48 -0800
Subject: [PATCH] zip code validation complete

---
 src/Form/ModuleConfigurationForm.php | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/Form/ModuleConfigurationForm.php b/src/Form/ModuleConfigurationForm.php
index caf6075..50efd73 100644
--- a/src/Form/ModuleConfigurationForm.php
+++ b/src/Form/ModuleConfigurationForm.php
@@ -117,20 +117,21 @@ class ModuleConfigurationForm extends ConfigFormBase {
     $form['contact']['zip'] = array(
       '#type' => 'textfield',
       '#title' => $this->t('Zip Code'),
+      '#description' => $this->t('Zip Code – formatted as 43210 or 43210-1234'),
       '#default_value' => $config->get('zip'),
     );
 
     $form['contact']['phone'] = array(
       '#type' => 'tel',
       '#title' => $this->t('Phone'),
-      '#description' => $this->t('Main contact phone number for your site – formatted as 555-555-5555'),
+      '#description' => $this->t('Main contact phone number for your site – formatted as 614-555-5555'),
       '#default_value' => $config->get('phone'),
     );
 
     $form['contact']['fax'] = array(
       '#type' => 'tel',
       '#title' => $this->t('Fax'),
-      '#description' => $this->t('Fax number, if applicable – formatted as 555-555-5555'),
+      '#description' => $this->t('Fax number, if applicable – formatted as 614-555-5555'),
       '#default_value' => $config->get('fax'),
     );
 
@@ -259,7 +260,15 @@ class ModuleConfigurationForm extends ConfigFormBase {
   public function validateForm(array &$form, FormStateInterface $form_state) {
     $values = $form_state->getValues();
 
-    // phone numbers
+    // zip code fields
+    $zipCodes = ['zip'];
+    foreach ($zipCodes as $zipCode) {
+      if (!empty($values[$zipCode]) && !preg_match('/^\d{5}(?:[-\s]\d{4})?$/i', $values[$zipCode])) {
+        $form_state->setErrorByName($zipCode, $this->t('The Zip Code is not in the proper format.'));
+      }
+    }
+
+    // phone number fields
     $phoneNumbers = ['phone', 'fax'];
     foreach ($phoneNumbers as $number) {
       if (!empty($values[$number]) && !preg_match('/^\d{3}-\d{3}-\d{4}$/i', $values[$number])) {
-- 
GitLab