Form not sending - Please enter a valid postal zone

I have added extra fields to a form including postal zones. The form does not send/ work because it gives the error ‘Please enter a valid postal zone’ which is entered.

<div class="field">
				<label for="postal-zone">
					Postal-zone <abbr title="required">*</abbr>
				</label>
				<select name="Postal-zone">
				 <option value="UK">UK</option>
				  <option value="Europe">Europe Zone 1/2/3</option>
				  <option value="Zone 1">World Zone 1</option>
				  <option value="Zone 2">World Zone 2</option>
				  <option value="Zone 3">World Zone 3</option>
				  </select>
				<?= isset($alert['postal-zone']) ? '<span class="alert error">' . esc($alert['postal-zone']) . '</span>' : '' ?>
			</div>
$rules = [
			'firstname'  => ['required', 'minLength' => 3],
			'lastname'  => ['required', 'minLength' => 3],
			'email' => ['required', 'email'],
			'address' => ['required','minLength' => 3],
			'address' => ['required', 'minLength' => 3],
			'postal-code' => ['required',  'minLength' => 3],
			'city' => ['required', 'minLength' => 3],
			'country' => ['required', 'minLength' => 3],
			'postal-zone'=> ['required', 'minLength' => 3],
			'text'  => ['required', 'minLength' => 3, 'maxLength' => 3000],
		];

I’d say your validation rule for postal-zone doesn’t make sense, value UK has only two letters. The proper way to check a select, is to check against an array of allowed values.

Additionally, the name of the select starts with an uppercase letter, make sure this is not the case.

Same for other fields, there are first and last names, cities that have only two letters (Ma, Hu).

Why when you put in a valid address does it keep saying ‘Please enter a valid address’

I don’t know, you didn’t add the full form, but you do have the address field twice in your $rules array.

It would be helpful if you tell us if the information provided solved the issue. Thank you.

Now when I try and send the form I get this message:

This page is currently offline due to an unexpected error. We are very sorry for the inconvenience and will fix it as soon as possible. Debug mode is enabled but does not show anything.

Advice for developers and administrators:
Enable debug mode to get further information about the error.

It would be helpful if you tell us if the information provided solved the issue. no.

I cannot help you with the information provided. Maybe you have an error in your config file, if debug is really enabled and no error shows up.

I removed all my code and just used what is here Email contact form | Kirby CMS. When you fill in the form and press submit the data empties and nothing else happens.

In the config file. The file is in a folder called config/config.php and not in site/config for some reason. I moved it but it just puts the site offline.

return [
  'markdown' => [
    'extra' => true,
    'debug'  => true
    ]
  ]
];

The syntax is wrong, debug needs to be on the same level as markdown, not nested inside the markdown array.

I removed that file and added a new one and added into site/config folder but the form still does not send.

return [
    'debug' => true,
];

The form is sending now but I don’t get any data in the email except Hello. I have to have this working probably for tomorrow.

The contact form code

<?php snippet("meta-book"); ?>

<?php snippet("header"); ?>

	<main class="main">
		<?= $page->text()->kirbytext() ?>
		

		<?php if($success): ?>
		<div class="alert success">
			<p><?= $success ?></p>
		</div>
		<?php else: ?>
		<?php if (isset($alert['error'])): ?>
			<div><?= $alert['error'] ?></div>
		<?php endif ?>
		<form method="post" action="<?= $page->url() ?>">
			
			<div class="field">
				<label for="name">
					Name <abbr title="required">*</abbr>
				</label>
				<input type="text" id="name" name="name" placeholder= "Name" value="<?= esc($data['name'] ?? '', 'attr') ?>" required>
				<?= isset($alert['name']) ? '<span class="alert error">' . esc($alert['name']) . '</span>' : '' ?>
			</div>
			<div class="field">
				<label for="email">
					Email <abbr title="required">*</abbr>
				</label>
				<input type="email" id="email" name="email" placeholder="Email" value="<?= esc($data['email'] ?? '', 'attr') ?>" required>
				<?= isset($alert['email']) ? '<span class="alert error">' . esc($alert['email']) . '</span>' : '' ?>
			</div>
			
			<div class="field">
				<label for="address">
					Address <abbr title="required">*</abbr>
				</label>
				
				<input type="text" id="address" name="address" placeholder="Address" value="<?= esc($data['address'] ?? '', 'attr') ?>" required>
				<?= isset($alert['address']) ? '<span class="alert error">' . esc($alert['address']) . '</span>' : '' ?>
			</div>
			
			<div class="field">
				<label for="postal-code">
					Postal/Zip Code <abbr title="required">*</abbr>
				</label>
				
				<input type="text" id="postal-code" name="postal-code" placeholder="Postal Code" value="<?= esc($data['postal-code'] ?? '', 'attr') ?>" required>
				<?= isset($alert['postal-code']) ? '<span class="alert error">' . esc($alert['postal-code']) . '</span>' : '' ?>
			</div>
			
			<div class="field">
				<label for="city">
					City/Town <abbr title="required">*</abbr>
				</label>
				
				<input type="text" id="city" name="city" placeholder="City/Town" value="<?= esc($data['city'] ?? '', 'attr') ?>" required>
				<?= isset($alert['city']) ? '<span class="alert error">' . esc($alert['city']) . '</span>' : '' ?>
			</div>
			
			<div class="field">
				<label for="text">
					Text <abbr title="required">*</abbr>
				</label>
				<textarea id="text" name="text" required>
					<?= esc($data['text'] ?? '') ?>
				</textarea>
				<?= isset($alert['text']) ? '<span class="alert error">' . esc($alert['text']) . '</span>' : '' ?>
			</div>
			<input type="submit" name="submit" value="Submit">
		</form>
		<?php endif ?>
	</main>

<?php snippet('footer') ?>

The controllers form code

<?php
return function($kirby, $pages, $page) {

	$alert = null;

	if($kirby->request()->is('POST') && get('submit')) {

		// check the honeypot
		if(empty(get('website')) === false) {
			go($page->url());
			exit;
		}

		$data = [
			'name'  => get('name'),
			'email' => get('email'),
			'address' => get('address'),
			'postal-code' => get('postal-code'),
			'city' => get('city'),
			'text' => get('text')
		];

		$rules = [
			'name'  => ['required', 'minLength' => 3],
			'email' => ['required', 'email'],
			'address' => ['required', 'minLength' => 3],
			'postal-code' => ['required', 'minLength' => 3],
			'city' => ['required', 'minLength' => 3],
			'text' => ['required', 'minLength' => 3, 'maxLength' => 3000],
		];

		$messages = [
			'name'  => 'Please enter a valid name',
			'email' => 'Please enter a valid email address',
			'address' => 'Please enter a valid address',
			'postal-code' => 'Please enter a valid postal-code',
			'city' => 'Please enter a valid city',
			'text'  => 'Please enter a text between 3 and 3000 characters'
		];

		// some of the data is invalid
		if($invalid = invalid($data, $rules, $messages)) {
			$alert = $invalid;

			// the data is fine, let's send the email
		} else {
			try {
				$kirby->email([
					'template' => 'email',
					'from'     => 'yourcontactform@yourcompany.com',
					'replyTo'  => $data['email'],
					'to'       => 'davidroberts8@me.com',
					'subject'  => esc($data['name']) . ' sent you a message from your contact form',
					'data'     => [
						'text'   => esc($data['text']),
						'sender' => esc($data['name'])
					]
				]);

			} catch (Exception $error) {
				if(option('debug')):
					$alert['error'] = 'The form could not be sent: <strong>' . $error->getMessage() . '</strong>';
				else:
					$alert['error'] = 'The form could not be sent!';
				endif;
			}

			// no exception occurred, let's send a success message
			if (empty($alert) === true) {
				$success = 'Your message has been sent, thank you. We will get back to you soon!';
				$data = [];
			}
		}
	}

	return [
		'alert'   => $alert,
		'data'    => $data ?? false,
		'success' => $success ?? false
	];
};

The form is sending now but I don’t get any data in the email except Hello.

This still does not work! Is there any chance of this working or is the software somehow not working?

Please refer to the original recipe how to pass data to your email template. If you don’t pass the data to the email template and don’t include the variables in the template, they cannot be sent.

Here I see only text and sender. Your email template is missing from your code snippets.