Validating Request bodies
When handling a request we want to easily verify, if our incoming data contains all the stuff we want and only the stuff we want. For this reason, FMVC utilizes the Dry::PHP library to validate request bodies against a predefined model.
For each action defined in routes.php
an individual model can be declared.
The basics
At first we need to define a model. Our validation models are being stored in Validation\
.
use Dry\DryStruct;
class User extends DryStruct
{
public function __construct() {
parent::__construct();
$this::optional('id')->filled('integer');
$this::required('username')->filled('string');
$this::requried('firstname')->filled('string');
$this::optional('lastname')->filled('string');
}
}
A property can be declared as either required
or optional
. It is important to declare all optional fields as the validation will point out all additional fields occurring in the used payload, that are not being declared in the model it will validte against.