Validate or remove for extra fields in laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Laravel provides various ways to handle form requests with additional fields or remove extra ones, ensuring clean data handling and error-free processes. In this blog post, we will discuss several approaches for addressing extra fields during the validation process.
1. Validate or Ignore Unknown Fieldsphp
public function rules() {
return [
'id' => 'required|integer',
'company_name' => 'required|max:255',
];
}
public function attributes() {
return [
'_unknown_fields' => 'These fields are unknown and will be ignored.'
];
}
// In the controller
if($request->has('_unknown_fields')) {
// Handle this case if needed, but for example purpose, we ignore them.
}php
public function additionalRules() {
return [
'_unknown_fields.*' => 'blacklisted', // blacklist fields starting with '_unknown_fields.'
];
}php
public function rules() {
return [
'id' => 'required|integer',
'company_name' => 'required|max:255',
];
}
// In the controller
$data = $request->validate([
... // Existing validation rules for primary fields
'_unknown_fields.*' => ['nullable', Rule::custom(function($attribute, $_unknown_fields) {
throw new \Exception('Unknown fields are not allowed in this request.');
})],
]);php
public function rules() {
return [
'id' => 'required|integer',
'company_name' => 'required|max:255',
];
}
// In the controller
$data = request()->except(['_unknown_fields.*']);php
public function handle($request, Closure $next) {
// Remove unwanted fields from the request payload
$validated = $request->validate([
... // Existing validation rules for primary fields
]);
$fieldsToRemove = ['_unknown_fields.*'];
foreach ($fieldsToRemove as $field) {
if (array_key_exists($field, $request->all())) {
unset($request->$field);
}
}
return $next($request);
}