Skip to content

Commit 60b7253

Browse files
committed
Add documentation for form validation in templates
1 parent d8778b0 commit 60b7253

14 files changed

Lines changed: 320 additions & 136 deletions

File tree

docs/_tips/form-validation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TIP: **Tip:** This form utilizes template [form validation and error handling](/templates/form-validation.md). Refer to the documentation for additional parameters and variables that are available to this tag.

docs/add-ons/email.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ The contact form is created similar to a standard web form, only you **do not**
5252
</p>
5353
{/exp:email:contact_form}
5454

55+
{{embed:_tips/form-validation.md}}
56+
5557
## Parameters
5658

5759
[TOC=3]
@@ -330,7 +332,7 @@ In the above example, the Template "friend" contains the Tell-a-Friend form.
330332
[TOC=3]
331333

332334
### `allow_attachments=`
333-
335+
334336
allow_attachments="yes"
335337

336338
This allows you to add a file input field to your form, make sure to give your file input field the name of `attachment`. Adding this parameter automatically gives the form the `enctype='multipart/form-data'` attribute.

docs/comment/form.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The comment submission form is created very similar to a standard web form, only
3535

3636
<input type="submit" name="submit" value="Submit" />
3737
<input type="submit" name="preview" value="Preview" />
38-
38+
3939
{!-- required to prevent EE from outputting form if commenting is disabled or expired --}
4040
{if comments_disabled}Comments on this entry are currently disabled.{/if}
4141
{if comments_expired}Commenting on this entry has expired.{/if}
@@ -46,6 +46,8 @@ This form should be placed on a "single-entry" type page such as a comments page
4646

4747
TIP: **Tip:** Notice the variables in the "value" form fields? These allow us to show the user's information in the form automatically if they click the "remember personal info" option.
4848

49+
{{embed:_tips/form-validation.md}}
50+
4951
## Comment Form Tag
5052

5153
### Parameters
@@ -227,11 +229,11 @@ A request for an edit will return a response array. In the case of an error, an
227229

228230
### Editing Permissions
229231

230-
By using the [{if editable}](/comment/entries.html#if-editable) conditional in the Comment Entries tag, you can output a link, instructions or a form if the viewing member has permission to edit the comment, and by using the {if can_moderate_comment} you can display whatever is appropriate if the viewing member has permission to moderate (close) the comment.
232+
By using the [{if editable}](/comment/entries.md#if-editable) conditional in the Comment Entries tag, you can output a link, instructions or a form if the viewing member has permission to edit the comment, and by using the {if can_moderate_comment} you can display whatever is appropriate if the viewing member has permission to moderate (close) the comment.
231233

232234
For members without administrative access, in order to edit a comment they must be logged in, the author of the comment, and the editing time limit must not have expired. If a member has a role with permission to edit the comments of any entry, that member will have edit permissions regardless of the editing time limit.
233235

234-
Comment moderators may close the comment. The edit time limit does not apply to moderators.
236+
Comment moderators may close the comment. The edit time limit does not apply to moderators.
235237

236238
Superadmins will always have {editable} and {can_moderate_comment} permissions on any comment.
237239

docs/development/legacy/libraries/output.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,78 @@ $output = array(
195195
'message' => 'not allowed',
196196
);
197197
ee()->output->send_ajax_response($output, true);
198+
199+
### `show_message($data, $xhtml = true, $redirect_url = false, $template_name = 'generic')`
200+
201+
| Parameter | Type | Description |
202+
| --------------- | -------- | ------------------------------------------------------------------------------------------ |
203+
| \$data | `Array` | Data to be sent to the view. Explained below |
204+
| \$xhtml | `Bool` | Parse the content as HTML |
205+
| \$redirect_url | `String` | URL to redirect to instead of showing the message |
206+
| \$template_name | `String` | Specialty template to use. Defaults to `generic` which is equivalent of `message_template` |
207+
| Returns | `Void` | void |
208+
209+
Show user message using [system message template](control-panel/template-manager.md#system-message-templates) or [custom template](control-panel/template-manager.md#custom-system-messages). This is used on front-end, for instance, when we need to inform the user about successful form submission.
210+
211+
When `$redirect_url` is provided, the user will be redirected to that URL instead of showing the message.
212+
213+
The `$data` parameter is an associative array with the following keys:
214+
- `title` - HTML titles of message page
215+
- `heading` - heading text
216+
- `content` - main content
217+
- `redirect` - URL to automatically redirect to after showing message
218+
- `rate` - time in seconds after which the redirect happens
219+
- `link` - the link to include in the message. Should be in the format of `array($link_url, $link_text)`
220+
221+
Example:
222+
223+
$data = array(
224+
'title' => 'Title',
225+
'heading' => 'Heading',
226+
'content' => 'Content',
227+
'redirect' => 'http://example.com',
228+
'rate' => 5,
229+
'link' => array('http://example.com', 'Link Text')
230+
);
231+
ee()->output->show_message($data);
232+
233+
### `show_user_error($type = 'submission', $errors = '', $heading = '', $redirect_url = '')`
234+
235+
| Parameter | Type | Description |
236+
| --------------- | -------- | ------------------------------------------------------------------------------------------ |
237+
| \$type | `String` | Type of error. Defaults to `submission`, can also be `general` or `off` |
238+
| \$errors | `Mixed` | Error message or array of messages to display. |
239+
| \$heading | `String` | Heading text. Legacy, not used, set automatically based on type. |
240+
| \$redirect_url | `String` | URL to redirect to instead of showing the message |
241+
| Returns | `Void` | void |
242+
243+
Show user an error message using [system message template](control-panel/template-manager.md#system-message-templates) or [custom template](control-panel/template-manager.md#custom-system-messages). This is used on front-end, for instance, when a form is submitted without the required info.
244+
245+
When `$redirect_url` is provided, the user will be redirected to that URL instead of showing the message. Useful when the form is set to [handle errors inline](templates/globals/single-variables.md#error-variables).
246+
247+
$return_error_link = ee()->functions->determine_error_return();
248+
ee()->output->show_user_error('submission', $errors, '', $return_error_link);
249+
250+
### `show_form_error($errors, $type = 'general')`
251+
252+
| Parameter | Type | Description |
253+
| --------------- | -------- | ------------------------------------------------------------------------------------------ |
254+
| \$errors | `Mixed` | Array of error messages or instance of \ExpressionEngine\Service\Validation\Result |
255+
| \$type | `String` | Type of error. Defaults to `general`, can also be `submission` or `off` |
256+
| Returns | `Void` | void |
257+
258+
Handle displaying errors for a form either passed as a Validation Result or an array of errors. If the form is using
259+
inline error handling the errors and old input will be flashed to the session otherwise they will be displayed with the
260+
appropriate template.
261+
262+
### `show_form_error_aliases($errors = '', $aliases = [], $type = 'general')`
263+
264+
| Parameter | Type | Description |
265+
| --------------- | -------- | --------------------------------------------------------------------------------------------- |
266+
| \$errors | `Mixed` | Array of error messages or instance of \ExpressionEngine\Service\Validation\Result |
267+
| \$aliases | `Array` | Array of aliases to use, i.e. ['input_id_1' => ['field' => 'input_name', 'label' => 'Input']] |
268+
| \$type | `String` | Type of error. Defaults to `general`, can also be `submission` or `off` |
269+
| Returns | `Void` | void |
270+
271+
This function builds upon `show_form_error()` with the additional ability to provide aliases for input names so
272+
that variable names used in inline error handling can be more meaningful i.e. `field_name` instead of `field_id_1`.

docs/member/edit-profile.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
This template tag allows editing a member's profile using the form that is similar to [Channel Form](channels/channel-form/overview.md). Please note however that not all Channel Form parameters and template tags are available in the Member Profile form, so make sure to consult the documentation below. The form can only be used to update profile of the member that is currently logged in.
1919

20+
{{embed:_tips/form-validation.md}}
21+
2022
## Parameters
2123

2224
{{embed:_tips/form-attributes.md}}
@@ -194,6 +196,7 @@ Short name of the fieldtype used for field
194196
{exp:member:edit_profile
195197
return="member/registration/success"
196198
include_assets="yes"
199+
inline_errors="yes"
197200
datepicker="yes"
198201
}
199202

@@ -204,27 +207,42 @@ Short name of the fieldtype used for field
204207
<p>
205208
<label for="username">Username*:</label><br />
206209
<input type="text" name="username" id="username" value="{username}"/><br />
210+
{if error:username}
211+
<span class="error">{error:username}</span>
212+
{/if}
207213
</p>
208214

209215
<p>
210216
<label for="email">Email:</label><br />
211217
<input type="text" name="email" id="email" value="{email}"/><br />
218+
{if error:email}
219+
<span class="error">{error:email}</span>
220+
{/if}
212221
</p>
213222

214223
<p>
215224
<label for="password">Password:</label><br />
216225
<input type="password" name="password" id="password" value=""/>
226+
{if error:password}
227+
<span class="error">{error:password}</span>
228+
{/if}
217229
</p>
218230

219231
<p>
220232
<label for="password_confirm">Confirm password*:</label><br />
221233
<input type="password" name="password_confirm" id="password_confirm" value=""/>
234+
{if error:password_confirm}
235+
<span class="error">{error:password_confirm}</span>
236+
{/if}
222237
</p>
223238

224239
<p>
225240
<label for="current_password">Current password*:</label><br />
226241
<em>You <b>must</b> enter your current password to change your password, username or email.</em>
227242
<input type="password" name="current_password" id="current_password" value=""/>
243+
{if error:current_password}
244+
<span class="error">{error:current_password}</span>
245+
{/if}
228246
</p>
229247

230248

@@ -235,6 +253,9 @@ Short name of the fieldtype used for field
235253

236254
{form:custom_profile_field}
237255

256+
{if has_error}
257+
<span class="error">{error}</span>
258+
{/if}
238259
</p>
239260
{/custom_profile_fields}
240261

docs/member/forgot-password.md

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@ Output a forgotten password form that sends an email with instructions for reset
2424

2525
{/exp:member:forgot_password_form}
2626

27-
27+
2828
NOTE: **Note:** This form will only email the user if the user requesting the password reset is not currently logged in.
2929

30+
{{embed:_tips/form-validation.md}}
31+
3032
## Parameters
3133

3234
### `email_template=`
3335

3436
email_template="member/email-password-reset"
3537

36-
Template to use for email which is sent to user.
38+
Template to use for email which is sent to user.
3739

3840
NOTE: **Note:** If no template is defined, the default [Member Profile Template](control-panel/template-manager.md#member-profile-templates) for a forgotten password will be used.
3941

@@ -66,47 +68,27 @@ Member email address. This is a **required** field:
6668
<label for="email">Email</label>
6769
<input type="email" name="email" value="" maxlength="120" size="40" />
6870

69-
70-
71-
## Variable Pairs
72-
73-
### `{errors}`
74-
75-
Form submission errors are displayed using a "looping pair" as there can be more than 1 error in a form submission.
76-
77-
{errors}
78-
<p>{error}</p>
79-
{/errors}
80-
81-
#### Error Tag Pair Parameters
82-
83-
##### `backspace=`
84-
85-
backspace="3"
86-
87-
The `backspace=` parameter will remove characters, including spaces and line breaks, from the last iteration of the tag pair.
88-
89-
#### Error Tag Pair Variables
90-
91-
##### `{error}`
92-
93-
{error}
94-
95-
The error text.
96-
97-
98-
9971
## Example
10072

10173
{exp:member:forgot_password_form
10274
return="member/forgot-password/sent"
75+
inline_errors="yes"
10376
password_reset_url="member/reset-password"
10477
email_template="member/email-password-reset"
10578
}
10679

80+
{if errors}
81+
<fieldset class="error">
82+
<legend>Errors</legend>
83+
{errors}
84+
<p>{error}</p>
85+
{/errors}
86+
</fieldset>
87+
{/if}
88+
10789
<p>
10890
<label>Your Email Address</label><br />
109-
<input type="email" name="email" value="" maxlength="120" size="40" />
91+
<input type="email" name="email" value="{if old:email}{old:email}{/if}" maxlength="120" size="40" />
11092
</p>
11193

11294
<p><input type="submit" name="submit" value="Submit" /></p>

docs/member/forgot-username.md

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Output a forgotten username form that sends an email with instructions for addre
2424

2525
{/exp:member:forgot_username_form}
2626

27+
{{embed:_tips/form-validation.md}}
28+
2729
## Parameters
2830

2931
### `email_subject=`
@@ -37,7 +39,7 @@ Subject of email sent to user.
3739

3840
email_template="member/email-forgot-username"
3941

40-
Template to use for email which is sent to user.
42+
Template to use for email which is sent to user.
4143

4244
If no template is defined or if the template defined does not exist, the default [Member Profile Template](control-panel/template-manager.md#member-profile-templates) for a forgotten username will be used.
4345

@@ -59,46 +61,27 @@ Member email address. This is a **required** field:
5961
<label for="email">Email</label>
6062
<input type="email" name="email" value="" maxlength="120" size="40" />
6163

62-
63-
64-
## Variable Pairs
65-
66-
### `{errors}`
67-
68-
Form submission errors are displayed using a "looping pair" as there can be more than 1 error in a form submission.
69-
70-
{errors}
71-
<p>{error}</p>
72-
{/errors}
73-
74-
#### Error Tag Pair Parameters
75-
76-
##### `backspace=`
77-
78-
backspace="3"
79-
80-
The `backspace=` parameter will remove characters, including spaces and line breaks, from the last iteration of the tag pair.
81-
82-
#### Error Tag Pair Variables
83-
84-
##### `{error}`
85-
86-
{error}
87-
88-
The error text.
89-
90-
9164
## Example
9265

9366
{exp:member:forgot_username_form
9467
return="member/login/forgot-username"
68+
inline_errors="yes"
9569
email_subject="Your Username"
9670
email_template="member/email-forgot-username"
9771
}
9872

73+
{if errors}
74+
<fieldset class="error">
75+
<legend>Errors</legend>
76+
{errors}
77+
<p>{error}</p>
78+
{/errors}
79+
</fieldset>
80+
{/if}
81+
9982
<p>
10083
<label>Your Email Address</label><br />
101-
<input type="email" name="email" value="" maxlength="120" size="40" />
84+
<input type="email" name="email" value="{if old:email}{old:email}{/if}" maxlength="120" size="40" />
10285
</p>
10386

10487
<p><input type="submit" name="submit" value="Submit" /></p>

0 commit comments

Comments
 (0)