Browse Source

Update to Uniform v3

master
Lukas Bestle 5 years ago
parent
commit
6592193ed4
  1. 36
      site/controllers/contact.php
  2. 4
      site/languages/de.php
  3. 4
      site/languages/en.php
  4. 2
      site/patterns
  5. 2
      site/plugins/uniform
  6. 6
      site/snippets/email-templates/contact.php
  7. 7
      site/snippets/templates/contact.errors.php
  8. 21
      site/snippets/templates/contact.php
  9. 13
      site/templates/contact.json.php

36
site/controllers/contact.php

@ -1,21 +1,31 @@
<?php
use Uniform\Form;
return function($site, $pages, $page) {
$form = uniform('contact', [
'required' => [
'name' => '',
'_from' => 'email'
$form = new Form([
'name' => [
'rules' => ['required'],
'message' => l::get('contact.message.name-invalid', 'Please enter your name.')
],
'email' => [
'rules' => ['required', 'email'],
'message' => l::get('contact.message.email-invalid', 'Please enter a valid email address.')
],
'actions' => [
[
'_action' => 'email',
'to' => qdated(),
'sender' => 'codesignd form <no-reply@codesignd.de>',
'subject' => 'Message from the contact form (' . $site->title() . ')',
'snippet' => 'email-templates/contact'
]
]
'message' => []
]);
// Disable some Uniform features
$form->withoutRedirect()->withoutFlashing();
if(r::is('POST')) {
$form->emailAction([
'to' => qdated(),
'from' => 'codesignd form <no-reply@codesignd.de>',
'subject' => 'Message from the contact form (' . $site->title() . ')',
'snippet' => 'email-templates/contact'
]);
}
return compact('form');
};

4
site/languages/de.php

@ -20,4 +20,8 @@ l::set('contact.potty', 'Bitte lasse dieses Feld leer');
l::set('contact.submit', 'Absenden');
l::set('contact.heading.email', 'E-Mail');
l::set('contact.message.name-invalid', 'Bitte geb deinen Namen ein.');
l::set('contact.message.email-invalid', 'Bitte geb eine gültige E-Mail-Adresse ein.');
l::set('contact.message.success', 'Danke für deine Nachricht, ich melde mich bald bei dir.');
l::set('qdated.error', 'Bei der Anzeige der E-Mail-Adresse ist ein Fehler aufgetreten. Bitte kontaktiere mich über <a href="https://twitter.com/lukasbestle">Twitter</a> und mache mich darauf aufmerksam. Danke!');

4
site/languages/en.php

@ -20,4 +20,8 @@ l::set('contact.potty', 'Please leave this field blank');
l::set('contact.submit', 'Submit');
l::set('contact.heading.email', 'Email');
l::set('contact.message.name-invalid', 'Please enter your name.');
l::set('contact.message.email-invalid', 'Please enter a valid email address.');
l::set('contact.message.success', 'Thank you for your message, I will get back to you soon.');
l::set('qdated.error', 'An error occured while displaying the email address. Please contact me via <a href="https://twitter.com/lukasbestle">Twitter</a> and tell me about this. Thank you!');

2
site/patterns

@ -1 +1 @@
Subproject commit 35c60ca0d91a6e3def4d05638fdab3ad4e523c80
Subproject commit 12acbfc16b22be16e4d15a85607e38a7e482e129

2
site/plugins/uniform

@ -1 +1 @@
Subproject commit 2e6054672ef3a592d8a8aff264014fc6f7e9088b
Subproject commit dcdc51f44eb99d9f802b544f7408434e4ee4c0e8

6
site/snippets/email-templates/contact.php

@ -1,6 +1,6 @@
Name: <?= a::get($form, 'name') . "\n" ?>
Email: <?= a::get($form, '_from') . "\n" ?>
Name: <?= a::get($data, 'name') . "\n" ?>
Email: <?= a::get($data, 'email') . "\n" ?>
----
<?= a::get($form, 'message') ?>
<?= a::get($data, 'message') ?>

7
site/snippets/templates/contact.errors.php

@ -0,0 +1,7 @@
<?php if(count($form->errors()) > 0): ?>
<ul>
<?php foreach($form->errors() as $message): ?>
<li><?= implode("\n", $message) ?></li>
<?php endforeach ?>
</ul>
<?php endif ?>

21
site/snippets/templates/contact.php

@ -3,35 +3,38 @@
<div class="contact">
<div class="contact__main">
<form class="contact__form form<?php e($form->successful(), ' form--success') ?>" action="<?= url::current() ?>" method="post">
<form class="contact__form form<?php e($form->success(), ' form--success') ?>" action="<?= url::current() ?>" method="post">
<div class="field field--required field--small">
<label class="field__label" for="name"><?= l::get('contact.name', 'Name') ?></label>
<input class="input field__input<?php e($form->hasError('name'), ' input--invalid') ?>" type="text" name="name" id="name" value="<?php $form->echoValue('name') ?>" placeholder=" " autocomplete="name" required/>
<input class="input field__input<?php e($form->error('name'), ' input--invalid') ?>" type="text" name="name" id="name" value="<?= $form->old('name') ?>" placeholder=" " autocomplete="name" required/>
</div>
<div class="field field--required field--small">
<label class="field__label" for="email"><?= l::get('contact.email', 'Email address') ?></label>
<input class="input field__input<?php e($form->hasError('_from'), ' input--invalid') ?>" type="email" name="_from" id="email" value="<?php $form->echoValue('_from') ?>" placeholder=" " autocomplete="email" required/>
<input class="input field__input<?php e($form->error('email'), ' input--invalid') ?>" type="email" name="email" id="email" value="<?= $form->old('email') ?>" placeholder=" " autocomplete="email" required/>
</div>
<div class="field">
<label class="field__label" for="message"><?= l::get('contact.message', 'Message') ?></label>
<textarea class="input field__input" rows="7" name="message" id="message"><?php $form->echoValue('message') ?></textarea>
<textarea class="input field__input" rows="7" name="message" id="message"><?= $form->old('message') ?></textarea>
</div>
<label class="vh" for="website"><?= l::get('contact.potty', 'Please leave this field blank') ?></label>
<input class="vh" type="text" name="website" id="website" autocomplete="off"/>
<?= honeypot_field(null, 'vh') ?>
<?= csrf_field() ?>
<div class="form__message"><?php if($form->hasMessage() && !$form->successful()): ?><?php $form->echoMessage() ?><?php endif ?></div>
<div class="form__message">
<?php snippet('templates/contact.errors', compact('form')) ?>
</div>
<button class="button form__submit" type="submit" name="_submit" value="<?php echo $form->token() ?>"<?php e($form->successful(), ' disabled')?>><?= l::get('contact.submit', 'Submit') ?></button>
<button class="button form__submit" type="submit" name="_submit"<?php e($form->success(), ' disabled')?>><?= l::get('contact.submit', 'Submit') ?></button>
<img class="contact__stamp" src="<?= url('assets/img/stamp.svg') ?>" alt="">
</form>
<div class="contact__thank-you">
<?php if($form->hasMessage() && $form->successful()): ?>
<?php $form->echoMessage() ?>
<?php if($form->success()): ?>
<?= l::get('contact.message.success', 'Thank you for your message, I will get back to you soon.') ?>
<?php endif ?>
</div>
</div>

13
site/templates/contact.json.php

@ -4,13 +4,20 @@
if(r::is('POST')) {
$fields = [];
foreach(r::data() as $field => $value) {
$fields[$field] = $form->hasError($field);
$fields[$field] = ($form->error($field) != []);
}
// Use error or success message depending on status
if($form->success()) {
$message = l::get('contact.message.success', 'Thank you for your message, I will get back to you soon.');
} else {
$message = snippet('templates/contact.errors', compact('form'), true);
}
$data = [
'success' => $form->successful(),
'success' => $form->success(),
'fields' => $fields,
'message' => $form->message()
'message' => $message
];
echo response::json($data);

Loading…
Cancel
Save