Skip to content

Commit b0f9a7e

Browse files
committed
docs: improve Form Validation Tutorial
- make view filenames lowercase - add routes - use BaseController instead of Controller - do not show validation errors when showing the form first - show input data when validation errors - use $helpers - remove loading URL helper
1 parent 43fae62 commit b0f9a7e

4 files changed

Lines changed: 66 additions & 29 deletions

File tree

user_guide_src/source/libraries/validation.rst

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ example.
6262
The Form
6363
========
6464

65-
Using a text editor, create a form called **Signup.php**. In it, place this
65+
Using a text editor, create a form called **signup.php**. In it, place this
6666
code and save it to your **app/Views/** folder::
6767

6868
<html>
@@ -75,29 +75,29 @@ code and save it to your **app/Views/** folder::
7575

7676
<?= form_open('form') ?>
7777

78-
<h5>Username</h5>
79-
<input type="text" name="username" value="" size="50" />
78+
<h5>Username</h5>
79+
<input type="text" name="username" value="<?= set_value('username') ?>" size="50">
8080

81-
<h5>Password</h5>
82-
<input type="text" name="password" value="" size="50" />
81+
<h5>Password</h5>
82+
<input type="text" name="password" value="<?= set_value('password') ?>" size="50">
8383

84-
<h5>Password Confirm</h5>
85-
<input type="text" name="passconf" value="" size="50" />
84+
<h5>Password Confirm</h5>
85+
<input type="text" name="passconf" value="<?= set_value('passconf') ?>" size="50">
8686

87-
<h5>Email Address</h5>
88-
<input type="text" name="email" value="" size="50" />
87+
<h5>Email Address</h5>
88+
<input type="text" name="email" value="<?= set_value('email') ?>" size="50">
8989

90-
<div><input type="submit" value="Submit" /></div>
90+
<div><input type="submit" value="Submit"></div>
9191

92-
</form>
92+
<?= form_close() ?>
9393

9494
</body>
9595
</html>
9696

9797
The Success Page
9898
================
9999

100-
Using a text editor, create a form called **Success.php**. In it, place
100+
Using a text editor, create a form called **success.php**. In it, place
101101
this code and save it to your **app/Views/** folder::
102102

103103
<html>
@@ -121,6 +121,14 @@ this code and save it to your **app/Controllers/** folder:
121121

122122
.. literalinclude:: validation/001.php
123123

124+
The Routes
125+
==========
126+
127+
Then add routes for the controller in **app/Config/Routes.php**:
128+
129+
.. literalinclude:: validation/039.php
130+
:lines: 2-
131+
124132
Try it!
125133
=======
126134

@@ -142,11 +150,15 @@ the **Validation class** inside. See :ref:`controllers-validating-data`.
142150
Explanation
143151
===========
144152

145-
You'll notice several things about the above pages:
153+
You'll notice several things about the above pages.
146154

147-
The form (**Signup.php**) is a standard web form with a couple of exceptions:
155+
signup.php
156+
----------
148157

149-
#. It uses a :doc:`form helper </helpers/form_helper>` to create the form opening. Technically, this
158+
The form (**signup.php**) is a standard web form with a couple of exceptions:
159+
160+
#. It uses a :doc:`form helper </helpers/form_helper>` to create the form opening
161+
and closing. Technically, this
150162
isn't necessary. You could create the form using standard HTML.
151163
However, the benefit of using the helper is that it generates the
152164
action URL for you, based on the URL in your config file. This makes
@@ -159,9 +171,15 @@ The form (**Signup.php**) is a standard web form with a couple of exceptions:
159171
This function will return any error messages sent back by the
160172
validator. If there are no messages it returns an empty string.
161173

162-
The controller (**Form.php**) has one method: ``index()``. This method
163-
uses the Controller-provided ``validate()`` method and loads the form helper and URL
164-
helper used by your view files. It also runs the validation routine.
174+
Form.php
175+
--------
176+
177+
The controller (**Form.php**) has one property: ``$helpers``.
178+
It loads the form helper used by your view files.
179+
180+
The controller has one method: ``index()``. This method returns
181+
the **signup** view to show the form when a non-POST request comes. Otherwise, it
182+
uses the Controller-provided ``validate()`` method. It also runs the validation routine.
165183
Based on whether the validation was successful it either presents the
166184
form or the success page.
167185

@@ -171,6 +189,7 @@ Add Validation Rules
171189
Then add validation rules in the controller (**Form.php**):
172190

173191
.. literalinclude:: validation/002.php
192+
:lines: 2-
174193

175194
If you submit the form you should see the success page or the form with error messages.
176195

user_guide_src/source/libraries/validation/001.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,28 @@
22

33
namespace App\Controllers;
44

5-
use CodeIgniter\Controller;
5+
use Config\Services;
66

7-
class Form extends Controller
7+
class Form extends BaseController
88
{
9+
protected $helpers = ['form'];
10+
911
public function index()
1012
{
11-
helper(['form', 'url']);
13+
if (strtolower($this->request->getMethod()) !== 'post') {
14+
return view('signup', [
15+
'validation' => Services::validation(),
16+
]);
17+
}
18+
19+
$rules = [];
1220

13-
if (! $this->validate([])) {
14-
return view('Signup', [
21+
if (! $this->validate($rules)) {
22+
return view('signup', [
1523
'validation' => $this->validator,
1624
]);
1725
}
1826

19-
return view('Success');
27+
return view('success');
2028
}
2129
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22

3-
if (! $this->validate([
3+
// ...
4+
5+
$rules = [
46
'username' => 'required',
57
'password' => 'required|min_length[10]',
68
'passconf' => 'required|matches[password]',
7-
'email' => 'required|valid_email',
8-
])) {
9-
// ...
10-
}
9+
'email' => 'required|valid_email',
10+
];
11+
12+
// ...
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// ...
4+
5+
$routes->get('form', 'Form::index');
6+
$routes->post('form', 'Form::index');
7+
8+
// ...

0 commit comments

Comments
 (0)