Consider a Laravel controller method that uses $request->validate() with rules. What is the default behavior if the validation fails?
public function store(Request $request) {
$validated = $request->validate([
'name' => 'required|string',
'email' => 'required|email'
]);
// ... store logic
}Think about how Laravel helps users fix form errors by showing messages.
When validation fails, Laravel automatically redirects the user back to the previous page. It flashes the validation errors and old input data to the session so the user can correct mistakes.
Choose the correct Laravel validation rule string to require an age field that must be numeric and at least 18.
Look for the correct Laravel rule names and syntax for minimum values.
The correct rules are numeric to ensure a number and min:18 to require a minimum value of 18. The other options use invalid rule names or syntax.
Given this Laravel controller code, what does $validated contain after validation?
public function update(Request $request) {
$validated = $request->validate([
'title' => 'required|string|max:255',
'content' => 'nullable|string'
]);
return $validated;
}Think about what $request->validate() returns after success.
The validate() method returns an array containing only the fields that passed validation with their values from the request.
Identify the syntax error in this validation rules array:
public function store(Request $request) {
$request->validate([
'email' => 'required|email',
'password' => 'required|min:8|max:20',
'age' => 'nullable|numeric|min:18'
]);
}Look carefully at the commas between array elements.
In PHP arrays, each element must be separated by a comma. The missing comma after the 'password' line causes a syntax error.
Laravel offers Form Request classes for validation. What is the main advantage of using Form Requests over inline $request->validate() calls?
Think about code organization and maintainability.
Form Requests encapsulate validation rules and authorization logic in dedicated classes, keeping controllers clean and enabling reuse.