Validation
Dox offers multiple approaches for validating the request data in your application.
class BlogController {
create(DoxRequest req) {
req.validate({
'title': 'required|string|alpha',
'body' : 'required|string',
'status' : 'required|in:active,draft',
});
....
}
}
{
"title" : "Dartondox",
"body" : "A perfect solution for your web backend development with dart, a comprehensive and versatile framework that offers a wide range of features to help you build powerful and scalable web applications.",
"status" : "active",
}
Tip
Multiple rules can be separated by a vertical bar, represented as " | ".
Custom validation message
class BlogController {
create(DoxRequest req) {
req.validate({
'title': 'required',
'email': 'required|email',
},
messages: {
'required' : 'the {field} is required',
'email' : 'the {value} is not valid email',
},
);
}
}
Info
The validator will automatically substitute {field}
and {value}
with the corresponding request field and its value.
Nested Validation
class AddressController {
create(DoxRequest req) {
req.validate({
'address.city': 'required',
'address.post_code': 'required|numeric',
'address.street.street_1': 'required',
'address.street.street_2': 'required',
});
....
}
}
{
"address" : {
"city" : "Bangkok",
"post_code" : "10110",
"street" : {
"street_1" : "Chok Choi 4",
"street_2" : "23/41 Main",
}
}
}
Nested with array
class ProductController {
create(DoxRequest req) {
req.validate({
'products.*.item': 'required',
'products.*.base_price': 'required|double',
'products.*.options.*.name': 'required',
'products.*.options.*.price': 'required',
});
....
}
}
{
"products" : [
{
"item": "Shoes",
"base_price": "20000",
"options": [
{"name" : "red", "price" : "21000"},
{"name" : "blue", "price" : "22000"},
]
},
{
"item": "Nike",
"base_price": "35000",
"options": [
{"name" : "green", "price" : "37000"},
{"name" : "purple", "price" : "38000"},
]
}
]
}
Rules
required
req.validate({
'name': 'required',
});
required_if
req.validate({
'name': 'required_if:status,active',
'status': 'in:active,inactive'
});
Info
Name is required if status is active.
required_if_not
req.validate({
'name': 'required_if_not:status,active',
'status': 'in:active,inactive'
});
Info
Name is required if status is inactive.
email
req.validate({
'user_email': 'email',
});
string
req.validate({
'title': 'string',
});
numeric
req.validate({
'price': 'numeric',
});
boolean
req.validate({
'is_guest': 'boolean',
});
integer
req.validate({
'price': 'integer',
});
array
req.validate({
'product_ids': 'array',
});
json
req.validate({
'product': 'json',
});
ip
req.validate({
'user_ip_address': 'ip',
});
alpha
req.validate({
'title': 'alpha',
});
alpha_dash
req.validate({
'order_number': 'alpha_dash',
});
alpha_numeric
req.validate({
'order_number': 'alpha_numeric',
});
date
req.validate({
'dob': 'date',
});
url
req.validate({
'return_url': 'url',
});
uuid
req.validate({
'user_id': 'uuid',
});
in
req.validate({
'status': 'in:active,inactive',
});
Info
Status field must be one of active or inactive status.
not_in
req.validate({
'status': 'not_in:active,inactive',
});
start_with
req.validate({
'title': 'start_with:dox',
});
Info
Title string must start with text dox
.
end_with
req.validate({
'title': 'end_with:framework',
});
Info
Title string must end with text framework
.
confirmed
Confirm can be used to verify password.
req.validate({
'password': 'confirmed',
});
{
"password" => "12345678"
"password_confirmation" => "12345678"
}
Info
By default, it will verify against the password_confirmation
field. However, you can also employ a custom name to confirm the password.
req.validate({
'password': 'confirmed:confirm_password'
});
{
"password" => "12345678"
"confirm_password" => "12345678"
}
image
request.validate({
'profile_pic': 'image',
});
request.validate({
'profile_pic': 'image:png,jpeg,gif,jpg',
});
file
request.validate({
'attachment': 'file',
});
request.validate({
'attachment': 'file:png,pdf,docx',
});
character
min_length
req.validate({
'title': 'min_length:20',
});
max_length
req.validate({
'title': 'max_length:100',
});
length_between
req.validate({
'title': 'length_between:20,100',
});
number
min
req.validate({
'price': 'min:20',
});
max
req.validate({
'price': 'max:100',
});
between
req.validate({
'price': 'between:20,100',
});
greater_than
req.validate({
'price': 'greater_than:20',
});
less_than
req.validate({
'price': 'less_than:100',
});