Validation
Lithe provides various ways to validate the data received by your application. Let's explore different methods available to ensure your data is correct before processing it.
Quick Validation
Lithe includes a variety of ready-to-use validation rules for your data.
To better understand how validation works in Lithe, let's go through a complete example of how to validate a form and display errors to the user. This practical guide will help you efficiently validate the data in your requests using Lithe.
Defining the Routes
use App\Http\Controllers\PostController;
$app = new \Lithe\App;
$app->route('/post/create')
->get([PostController::class, 'create'])
->post([PostController::class, 'store']);
$app->listen();
The GET route displays a form for the user to create a new blog post, while the POST route saves the post to the database.
Creating the Controller
namespace App\Http\Controllers;
use Lithe\Http\Request;
use Lithe\Http\Response;
class PostController
{
/**
* Displays the form to create a new blog post.
*/
public static function create(Request $req, Response $res)
{
return $res->view('post.create');
}
/**
* Stores a new blog post.
*/
public static function store(Request $req, Response $res)
{
// Validate and store the blog post...
}
}
Writing the Validation Logic
Now let's fill the store
method with the necessary logic to validate the new blog post. We'll use the validate
method available in the HTTP request object. This method requires 1 parameter: $rules
, which defines the validation rules for specific fields.
The validate
method returns an object with two methods: passed
, which returns a boolean indicating whether all data is correct based on the defined rules, and errors
, which is an array listing the fields that failed validation along with their corresponding error codes.
To better understand how to use the validate
method, see the example in the store
method:
/**
* Stores a new blog post.
*/
public static function store(Request $req, Response $res)
{
$validate = $req->validate([
'title' => 'required|max:255',
'body' => 'required',
'status' => 'in:published,draft,pending',
'author' => 'name'
]);
if (!$validate->passed()) {
$errors = $validate->errors();
foreach ($errors as $field => $code) {
switch ($code) {
case 1001:
// Handle required field error
break;
case 1010:
// Handle max length error for 'title'
break;
case 1014:
// Handle invalid values for 'status'
break;
case 1015:
// Handle invalid 'name' format
break;
// Add more cases as needed
}
}
}
// Continue storing the post if validation passes...
return $res->redirect('/posts');
}
Available Validation Rules with Corresponding Errors
Here's a list of all the available validation rules and their corresponding error codes:
- required (error code: 1001): The field is required.
- email (error code: 1002): The field must be a valid email address.
- url (error code: 1003): The field must be a valid URL.
- ip (error code: 1004): The field must be a valid IP address.
- number (error code: 1005): The field must be a number.
- integer (error code: 1006): The field must be an integer.
- boolean (error code: 1007): The field must be either
true
orfalse
. - min:length (error code: 1009): The field must have at least
length
characters. - max:length (error code: 1010): The field cannot exceed
length
characters. - range:min,max (error code: 1011): The field must be within the range of
min
tomax
. - dateFormat:format (error code: 1012): The field must match the specified date format
format
. - alphanumeric (error code: 1013): The field must be alphanumeric (only letters and numbers).
- in:values (error code: 1014): The field must be one of the specified values (e.g.,
in:published,draft
). - name (error code: 1015): The field must contain a valid name (specific patterns for names may be defined).