Skip to content
Snippets Groups Projects
Commit 021e5af7 authored by Melissa Miller's avatar Melissa Miller
Browse files

zip code validation complete

parent 421c833c
No related branches found
No related tags found
No related merge requests found
......@@ -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])) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment