Validating boolean with Laravel Validation

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Validating Boolean Inputs with Laravel Form Requests Body:

When building web applications, one of the essential aspects is making sure that submitted data is clean and valid. Users often have a tendency to input incorrect or irrelevant information, which can negatively impact your application's performance and user experience.

Creating Form Requests

To validate forms in Laravel, you must first create a form request class. A form request is essentially a validation class that helps you define rules for handling incoming data from your user-submitted web forms.

 'required|max:255',
'password' => 'required|min:8',
'remember_me' => 'in:true,false'
];
}
}
?>

Validating Boolean Inputs

In the above code snippet, we have created a form request class for a login form. We have defined validation rules for the username and password fields. The "remember_me" field is checked to be validated as boolean input. As Laravel's in rule only accepts arrays of non-boolean strings, you need to specify true and false as string values.

Improving Validation with Custom Functions

To make the validation more concise and practical, you can use custom functions. For instance:

 'required|max:255',
'password' => 'required|min:8',
'remember_me' => 'boolean'
];
}

/**
* Custom function for boolean validation.
* @param $attribute
* @param $value
* @return bool
*/
public function validateBoolean($attribute, $value) {
return in_array($value, ['true', 'false']);
}
}
?>

Conclusion

Summary: Laravel offers several ways to validate your forms and ensures clean data is handled. The best practice for validating boolean inputs in a Laravel application involves creating form requests, defining validation rules, and potentially using custom functions for more concise code.

Referencing our example login form with the "remember me" checkbox, you now have a well-structured and efficient way to validate the boolean input. For further information on Laravel's validation system, please refer to the official documentation provided by the Laravel Company at http://laravel.com/docs/validation#basic-usage