Introduction
Forms Location
your_theme_name>resources>views>forms
Any files created inside this directory can be included on a page using {% form "contact" %}
Create a Contact Form
Create your form in the forms location, for example contact.twig
:
<form action="{{ route('form', 'contact') }}" method="post">
{{ csrf_field() }}
<div>
<label for="name">Name:</label>
<input id="name" type="text" name="name" autocomplete="name" required placeholder="First Name" value="{{ old('name') }}">
</div>
<div>
<label for="name">Email Address:</label>
<input id="email" type="text" name="email" autocomplete="email" required placeholder="Email Address" value="{{ old('email') }}">
</div>
<div>
<label for="name">Your Message</label>
<textarea id="message" type="text" name="message" autocomplete="off" required placeholder="Your message">{{ old('message') }}</textarea>
</div>
<div>
<button type="submit">Submit</button>
</div>
</form>
Session message
{% if session('success') %}
<div role="alert">
<span>Your message has been sent.</span>
</div>
{% endif %}
Redirect to success page (optional)
Add the following to your project in app>providers>AppServiceProvider.php
public function boot()
{
\Aero\Store\Http\Responses\FormSubmit::extend(function ($builder) {
if ($builder->form === 'contact') {
$builder->setRedirect('/success');
}
});
}
Create success page
- Log in to the admin and navigate to
/admin/content/pages
- Create new page
- Set page name to success
- Add your content
Mail notification
- Log in to the admin and navigate to
/admin/configuration/mail
- Create new mail
- Set label to a name of your choice
- Select "System" layout
- Add the email address of the recipient
- Select "Form Submitted" from the event list
- Set this to match your route, in our case contact
- Continue to the next step
- Configure your email
- Set the subject to the subject of your email
- Input the snippet of code that will be outputted in the body of your email (see example below)
- Save
{% for field, value in fields %}
<p>{{ field }}: {{ value }}</p>
{% endfor %}
Test your email
You'll need to set up a mail server for example when testing locally you could use Mailtrap
Include your form on a contact page
- Log in to the admin and navigate to
/admin/content/pages
- Create a new page
- Set the name to what you'd like the title of your page to be
- In the content area add
{% form "contact" %}
where you'd like the form to display.
- Save