The recommended method to load this functionality is from within the server-side config.php
,
as this will define both backend functionality and frontend configuration.
return [
// ...
// Uncomment to enable forms
'forms' => [
'your-form-name' => [
'fields' => [
// ...
],
'actions' => [
// ...
],
],
],
];
This will auto-set the client-side initialization with the appropriate parameters.
Each field definition should be keyed with the field name and can contain the following fields:
type
- The type of fieldlabel
- The label to display for the fieldplaceholder
- The placeholder text for the fieldrequired
- True/False Whether the field is requiredvalue
- Default value when rendering the formoptions
- An array of options for select and checkbox fieldsThe keyname of the field should be form-safe, ie: no spaces contained and all lowercase.
Example field definitions:
'fields' => [
'name' => [
'label' => 'Your name',
'required' => true,
'value' => 'Default Value'
],
'email' => [
'type' => 'email',
'required' => true
],
'message' => [
'type' => 'textarea',
'required' => true,
'placeholder' => 'Enter your message here'
],
],
Type defines the input type and associated behaviour. Valid options:
text
- A single line text inputemail
- An email input, validation check for a valid email addressnumber
- A number inputtextarea
- A multi-line text inputselect
- A dropdown select input, use options
to define optionscheckbox
- A single checkbox inputradio
- A group of radio inputs, use options
to define optionscheckboxes
- A group of checkboxes, use options
to define optionshidden
- A hidden inputsubmit
- A submit button, use value
to set text displayed on the buttonRenders the label for the field, skipped for submit
and hidden
types.
Renders the placeholder text for the field for text, email, number, and textarea types.
Set to true
to require the field to be filled in before submission.
Sets the default value for the field when rendering the form. This value can be changed by the user.
For select
and radio
types will define the pre-selected option.
Sets the available options for select
, radio
, and checkboxes
input types.
'options' => [
'option1' => 'Option 1',
'option2' => 'Option 2',
'option3' => 'Option 3',
],
Actions set from server configuration are only used on the backend and are not transmitted to the client.
Basic test action that will return the received JSON object back to the client. This is useful in testing a form to ensure you are receiving the fields you expect.
'actions' => [
[
'action' => 'test',
],
]
Action to send an email to the specified address.
Important: using the email action requires that an email configuration is defined.
'actions' => [
[
'action' => 'email',
'to' => 'recipient@targetdomain.tld',
'subject' => 'Message Subject', // Optional
'template' => 'your_template', // Optional
],
]
Optionally the to
target address can be field:user_email
(or similar),
to use the field value with the name user_email
as the target address.
This allows you to send an email to the original submitter of the form, (presuming you collect their email in that form).
If a template is specified, a file lookup will be performed for
themes/{your_theme}/layouts/email/{template_name}.tpl
to populate the contents of the email.
This is a plain-text template that will use {{field_name}}
to inject the field values
into the message. eg:
New message received from {{user_email}}!
{{message}}
---
Received from IP {{ip}} and user agent {{user_agent}}.
Each field key from the form is available, as are the following system-defined values:
If no template is specified, (or the file does not exist), the raw values from the form will be printed in the email body.
N/A
<cms-form name="contact" success="pages/contact-thanks"></cms-form>
Will render the author's profile using the default layout template.
name
- The key name of the form to rendersuccess
- The page fragment to redirect to on successful submission/** <editor-fold desc="CMS Form"> **/
cms-form {
position: relative;
}
cms-form label {
display: block;
margin: 1rem 0 0.25rem;
}
cms-form input[type="text"],
cms-form input[type="email"],
cms-form input[type="password"],
cms-form select,
cms-form textarea {
width: calc(100% - 1em);
padding: 0.5em;
border: 1px solid #ccc;
border-radius: 0.25em;
}
cms-form input[type="submit"] {
background-color: var(--color-accent1);
color: white;
border: none;
padding: 0.5em 1em;
border-radius: 0.25em;
cursor: pointer;
}
cms-form .cms-form-option label {
display: inline-block;
margin: 0.5em 0 0 1em;
}
cms-form .required-note {
font-size: 0.75rem;
color: #fd4545;
}
cms-form input.error,
cms-form textarea.error {
border-color: #fd4545;
background-color: #ffdbdb;
}
cms-form .error-message {
color: #a20202;
transition: transform 0.5s ease;
transform: scaleY(1);
transform-origin: top;
padding-top: 0.5em;
}
cms-form .error-message.hidden {
transform: scaleY(0);
}
/** </editor-fold> **/