How to validate Laravel 5.4 radio button?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# How to Validate Radio Buttons in Laravel 5.4: A Practical Guide
As developers working with Laravel, one of the most common tasks we encounter is handling form submissions, especially when dealing with mutually exclusive choices like radio buttons. Setting up robust validation for these inputs can sometimes introduce subtle bugs, as demonstrated by the integrity constraint violation you encountered. This post will walk you through the correct, developer-centric way to validate radio button groups in Laravel 5.4 and ensure your data flows smoothly into the database.
## Understanding the HTML Foundation
Before diving into Laravel validation, we must establish a solid foundation with the HTML structure. Radio buttons are designed so that only one option within a group can be selected at any given time. The key to making this work seamlessly with backend validation is ensuring all radio buttons share the *same* `name` attribute. This allows Laravel to receive a single value for that specific field.
Here is how you structure your radio button group:
```html
```
Notice that both inputs share the same `name="gender"`. When a user selects "Male," only the input with `name="gender"` and `value="male"` will have its corresponding field populated in the request data.
## Implementing Robust Validation in Laravel
The true power of Laravel lies in its validation rules, which allow us to enforce these constraints cleanly before the data ever touches the database. For radio buttons, the ideal validation rule is the `in` rule. This rule ensures that the submitted value must be one of a predefined set of allowed values.
In your case, since the only valid selections are 'male' or 'female', we use the `in` rule:
```php
$this->validate(request(), [
'title' => 'required',
'body' => 'required',
'gender' => 'required|in:male,female' // The crucial validation for radio buttons
]);
```
### Why the `in` Rule is Essential
The `in:value1,value2,...` rule guarantees that the submitted value for the `gender` field must exactly match one of the listed options. If a user somehow submits an invalid string (e.g., if front-end logic failed), Laravel immediately stops the request and returns a validation error, preventing bad data from reaching your controller logic. This practice is fundamental to building secure applications, aligning perfectly with the principles taught by the Laravel community, which emphasizes defensive coding practices when handling user input.
## Handling Data Insertion and Preventing Integrity Errors
Your original issue stemmed not just from validation, but from how you were attempting to map the validated data to your model attributes:
```php
// Problematic mapping example from context:
$post->male = $request['male'];
$post->female = $request['female'];
```
When you use `in:male,female`, only *one* of those values will be present in the request data (either `'male'` or `'female'`), and the other will be absent. If your database schema strictly requires both `male` and `female` columns to have non-null values upon insertion, trying to assign a missing value (like an empty string or `null`) can trigger the `SQLSTATE[23000]: Integrity constraint violation`.
### The Correct Approach: Conditional Assignment
Instead of blindly assigning all potential fields, you should check which radio button was actually selected and only populate the corresponding database column. This makes your code safer, more explicit, and directly addresses the data integrity issue.
Here is how you safely map the validated input:
```php
// Inside your controller method after successful validation
$post = new Post;
$post->title = $request['title'];
$post->body = $request['body'];
$post->status = $request['status'];
// Safely determine the gender based on what was submitted
if ($request->has('gender')) {
$gender = $request['gender'];
if ($gender === 'male') {
$post->male = true; // Or whatever appropriate boolean/value you use
$post->female = false;
} elseif ($gender === 'female') {
$post->male = false;
$post->female = true;
}
} else {
// Handle the case where gender wasn't selected, if necessary
$post->male = null;
$post->female = null;
}
$post->save();
return redirect('blog');
```
By explicitly checking which radio button was chosen, you ensure that only relevant data is inserted into the database. This approach prevents unexpected `NULL` values from causing integrity errors and results in cleaner, more predictable data persistence. For further insights on structuring your models and requests in Laravel, always refer to the official documentation found at [laravelcompany.com](https://laravelcompany.com).
## Conclusion
Validating radio buttons in Laravel involves a three-pronged approach: correct HTML setup, precise use of validation rules like `in`, and careful, conditional data mapping in your controller. By focusing on validating the *selection* first and then explicitly processing the result before saving to the database, you eliminate ambiguity and prevent those frustrating integrity constraint errors. Mastering this flow is a key step toward writing robust and maintainable Laravel applications.