Validate incoming request data with a fluent, Laravel-style syntax. Rules are defined as pipe-separated strings and errors are automatically collected per field.
@POST(value = "/register", name = "register.store")
private Object store(Request req, Response res) {
Map<String, Object> data = RequestValidator.validate(req, Map.of(
"username", "required|min:3|max:20|alphanumeric",
"email", "required|email|unique:users,email",
"password", "required|min:8|confirmed"
));
// data contains only validated fields
return render("dashboard.html", Map.of());
}
Use validateSafe() to get a ValidationResult instead of catching an exception.
@POST(value = "/register", name = "register.store")
private Object store(Request req, Response res) {
ValidationResult result = RequestValidator.validateSafe(req, Map.of(
"username", "required|min:3|max:20",
"email", "required|email",
"password", "required|min:8"
));
if (result.fails()) {
return render("register.html", Map.of(
"errors", result.getErrors(),
"old", req.queryMap().toMap()
));
}
Map<String, Object> data = result.getData();
// ...
}
| Rule | Description |
|---|---|
| required | Field must not be null or empty |
| Must be a valid email address | |
| min:8 | Minimum 8 characters |
| max:255 | Maximum 255 characters |
| between:3,20 | Length or value between 3 and 20 |
| numeric | Must be a number |
| integer | Must be an integer |
| alpha | Letters only |
| alphanumeric | Letters and numbers only |
| url | Must be a valid URL |
| confirmed | Must match field_confirmation |
| unique:users,email | Must not exist in table/column |
| in:admin,editor,user | Must be one of the listed values |
| regex:^[A-Z]+$ | Must match the given pattern |
The error() and old() helpers are available in all Pebble templates when you pass errors and old to the view.
<form method="POST" action="/register">
{{ csrf_field() | raw }}
<input
type="text"
name="username"
value="{{ old('username') }}"
>
{% if error('username') %}
<span class="error">{{ error('username') }}</span>
{% endif %}
<input
type="email"
name="email"
value="{{ old('email') }}"
>
{% if error('email') %}
<span class="error">{{ error('email') }}</span>
{% endif %}
<button type="submit">Register</button>
</form>
Use LiveComponentValidator to validate component state directly, without an HTTP request.
@LiveComponentImpl
public class RegisterForm extends LiveComponent
{
@State
private String username = "";
@State
private String email = "";
public void submit() {
ValidationResult result = LiveComponentValidator.validate(
Map.of("username", username, "email", email),
Map.of(
"username", "required|min:3|alphanumeric",
"email", "required|email"
)
);
if (result.fails()) {
// Handle errors
return;
}
// Process validated data
}
@Override
public String template() {
return "components/register-form.html";
}
}
validate() throws a ValidationException on failurevalidateSafe() returns a ValidationResult without throwingunique: checks against your ActiveJDBC models automatically