PHP Luminova: Validate Form Input and Check CSRF Tokens
Validate submitted fields, return useful errors, and protect browser form submissions with CSRF tokens.
Validate the shape and content of incoming fields before processing them. For browser forms that use session cookies, check the CSRF token before applying a change. See the Rule-Based Input Data Validation.
Input Validation
Separate rules with a pipe (|). Put an argument in parentheses, as in maxlength(10). The HTTP controller exposes its validator as $this->input.
Use addField() to add rules and messages for one field.
$this->input->addField('name', 'required|maxlength(10)|alphanumeric', [
'required' => 'Error message for required',
'maxlength' => 'Enter at most 10 characters.',
'alphanumeric' => 'Error message for alphanumeric'
]);Use setRules() to configure several fields and setMessages() to override default messages.
$rules = [
'name' => 'required|maxlength(10)|alphanumeric',
'age' => 'required|integer|minlimit(18)|maxlimit(120)',
'email' => 'required|email',
];
$errorMessages = [
'name' => [
'required' => 'Error message for required',
'maxlength' => 'Enter at most 10 characters.',
'alphanumeric' => 'Error message for alphanumeric'
],
'email' => [
'required' => 'The email field is required.',
'email' => 'Enter a valid email address.'
],
'age' => [
'required' => 'The age field is required.',
'integer' => 'The age must be an integer.',
'minlimit' => 'Enter an age of at least 18.',
'maxlimit' => 'Enter an age of at most 120.'
]
];
$this->input->setRules($rules)->setMessages($errorMessages);Input Validation Controller
Call validate() before processing the fields. This method only checks the submitted values. A login handler still needs to verify the credentials.
// /app/Controllers/Http/UserController.php
namespace App\Controllers\Http;
use Luminova\Base\Http\AbstractController;
class UserController extends AbstractController
{
public function userLogin(): int
{
$this->input->setRules([
'email' => 'required|email',
'password' => 'required|min(6)',
]);
$this->input->setMessages([
'email' => [
'required' => 'Email must not be empty',
'email' => 'Email address must be a valid email.'
],
'password' => [
'required' => 'Password must not be empty',
'min' => 'Password must not be less than 6 characters'
],
]);
if($this->input->validate($this->request)){
echo "Validation passed";
return STATUS_SUCCESS;
}
echo 'Validation failed';
echo $this->input->getError();
return STATUS_ERROR;
}
}Validate a Field with a Callback
Browsers submit form values as strings. This rule accepts a digit string as an ID and rejects values such as abc.
use Luminova\Security\Rule;
$this->input->addField('user_id', [
Rule::required(),
Rule::callback(
static fn (mixed $value, string $field): bool =>
is_int($value) || (is_string($value) && ctype_digit($value)),
'Enter a numeric user ID.'
),
]);Protect a Form with a CSRF Token
Render this form with the session active. Register the handler for the same POST path.
<?php use Luminova\Security\CSRF; ?>
<form method="post" action="/profile">
<?php CSRF::inputToken(); ?>
<label>Display name <input name="name" required></label>
<button type="submit">Save profile</button>
</form>In the POST handler, read the submitted token and verify it before updating the profile.
use Luminova\Security\CSRF;
$token = $this->request->getCsrfToken();
if ($token === null || !CSRF::validate($token)) {
http_response_code(403);
return STATUS_ERROR;
}
// Validate the submitted fields, then apply the update.A successful check consumes the token by default. Render a fresh token when displaying the form again. See CSRF Protection.