Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zxywvu's avatar

radio button required validation laravel

I have a form of gender.

<div class="col-md-6">
    <div class="form-check form-check-inline">
        <input class="form-check-input" type="radio" name="gender" id="sir" value="1">
        <label class="form-check-label" for="sir">sir</label>
    </div>
    <div class="form-check form-check-inline">
        <input class="form-check-input" type="radio" name="gender" id="lady" value="0">
        <label class="form-check-label" for="lady">lady</label>
    </div>
    @error('gender')
    <span class="invalid-feedback" role="alert">
        <strong>{{ $message }}</strong>
    </span>
    @enderror
</div>

Controller

protected function validator(array $data)
{
    return Validator::make($data, [
        'gender' => ['required', 'in:0,1'],
    ]);
}
0 likes
76 replies
SilenceBringer's avatar

@azadi ok. what's the problem?

We don't see the question (just some pieces of code), we don't know how you call validator function, we don't know what $message contains. How we can help you?

SilenceBringer's avatar

@azadi great. Can I see Register controller?

And THE MOST IMPORTANT question: what's wrong with your code? Any error?

zxywvu's avatar

@SilenceBringer

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\State;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Show the application registration form.
     *
     * @return \Illuminate\View\View
     */
    public function showRegistrationForm()
    {
        $states = State::all();
        return view('auth.register', compact('states'));
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'first_last_name' => ['required', 'string', 'max:255'],
            'mobile' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'national_ode' => ['required', 'number', 'max:12'],
            'state_id' => 'required',
            'city_id' => 'required',
            'address' => 'required',
            'gender' => ['required', 'in:0,1'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\Models\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
}

This code is not error

Sinnbeck's avatar

@azadi So it works? Great! Assume it was just to show off your code then?

zxywvu's avatar

@Sinnbeck It is not working. I want to show message validation when I did not check the gender.

Sinnbeck's avatar

@azadi Yes? You never call the validate method anywhere?? And what method are you calling with that form? You dont show the form part, so we have no way of knowing

Sinnbeck's avatar

@azadi This isnt a form

<div class="col-md-6">
    <div class="form-check form-check-inline">
        <input class="form-check-input" type="radio" name="gender" id="sir" value="1">
        <label class="form-check-label" for="sir">sir</label>
    </div>
    <div class="form-check form-check-inline">
        <input class="form-check-input" type="radio" name="gender" id="lady" value="0">
        <label class="form-check-label" for="lady">lady</label>
    </div>
    @error('gender')
    <span class="invalid-feedback" role="alert">
        <strong>{{ $message }}</strong>
    </span>
    @enderror
</div>

https://www.w3schools.com/html/html_forms.asp

Sinnbeck's avatar

@azadi Ok so the request is sent to {{ route('register') }}? Show that method in the controller. It should be a public method.

Sinnbeck's avatar

@azadi Can you show the route definition for that? And controller methods you want to use should be public

Sinnbeck's avatar

@azadi Nevermind. Make sure you actually hit that validator.

protected function validator(array $data)
    {
         dd('hit');
        return Validator::make($data, [
Snapey's avatar

@Sinnbeck it's looks like standard LaravelUi code, mostly handled in RegistersUsers trait

zxywvu's avatar

@Sinnbeck

^ array:12 [▼
  "_token" => "KxaqY0m7mDg9yfCmTyjF1iMIdtKopldf2JV2B3Ud"
  "name" => "aa"
  "first_last_name" => "aa"
  "email" => "[email protected]"
  "mobile" => "123"
  "password" => "123456789"
  "password_confirmation" => "123456789"
  "national_ode" => "123"
  "gender" => "1"
  "state_id" => "8"
  "city_id" => "87"
  "address" => "www"
]
zxywvu's avatar

@Sinnbeck

So what if it was empty? Or what if it is not selected? Or what if it is not even 1 or 0?

Snapey's avatar

@azadi have you tried setting the value to something else to see if it errors?

zxywvu's avatar

@Sinnbeck I see the validation error for everyone. But I don't see this radio button or I don't see it at all.

zxywvu's avatar

@Sinnbeck Like this

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'first_last_name' => ['required', 'string', 'max:255'],
        'mobile' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
        'national_ode' => ['required', 'number', 'max:12'],
        'state_id' => 'required',
        'city_id' => 'required',
        'address' => 'required',
        'gender\.0' => 'required',
        'gender\.1' => 'required',
    ]);
}
Sinnbeck's avatar

@azadi You didnt check the link I assume? Or you just scrolled to a different part of the docs? It specifically says bail

 'gender' => ['bail', 'required', 'in:0,1'],

Be sure to add it to all validation rules, if you want it to only show the first one that fails for some reason (still have no clue why you would want this)

Sinnbeck's avatar

@azadi From the docs i linked

Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute:

But maybe explain exactly what you want. You only gave a picture and I assumed you meant this.. But I have no way of knowing what you are thinking

zxywvu's avatar

@Sinnbeck When a user did not select the radio button, I want to see this

It is mandatory to fill in the gender option
Sinnbeck's avatar

@azadi Ah ok so it just doesnt show that validation message? Did you remember to check the data using dd() to ensure it is empty ??

zxywvu's avatar

@Sinnbeck

^ array:10 [▼
  "_token" => "KxaqY0m7mDg9yfCmTyjF1iMIdtKopldf2JV2B3Ud"
  "name" => null
  "first_last_name" => null
  "email" => null
  "mobile" => null
  "password" => null
  "password_confirmation" => null
  "national_ode" => null
  "state_id" => null
  "address" => null
]
Sinnbeck's avatar

@azadi Remove the bail again. I just misunderstood what you wanted then. But without it it should work. I cannot spot the error currently

zxywvu's avatar

@Sinnbeck I changed

'gender' => ['required', 'in:0,1'],

But I did not see a message of

It is mandatory to fill in the gender option

Sinnbeck's avatar

Well at least after 46 posts we can finally start working on the problem. Let me help you write what should have written in the very first post

I have this code inside a form. There is validation on all fields, and it works. For some reason I cannot get this radio input to fail validation, and show an error message. If I dont check any of the radio buttons, I dont get gender in the backend validation so I would assume it should show an error message

Sinnbeck's avatar

Can you try adding this, and see if that has the error message?

@foreach ($errors->all() as $key => $error)
    <li>{{$key}} {{ $error }}</li>
@endforeach 
zxywvu's avatar

@Sinnbeck

I see this

The name field is required.
The first last name field is required.
The mobile field is required.
The email field is required.
The password field is required.
The national ode field is required.
The state id field is required.
The city id field is required.
The address field is required.
The gender field is required.
Sinnbeck's avatar

@azadi So it works. Delete this and copy it from this page

@error('gender')
    <span class="invalid-feedback" role="alert">
        <strong>{{ $message }}</strong>
    </span>
    @enderror
zxywvu's avatar

@Sinnbeck

blade

   @error('gender')
    <span class="invalid-feedback" role="alert">
        <strong>{{ $message }}</strong>
    </span>
    @enderror 

MyController

 'gender' => ['required', 'in:0,1'],

But I see did not message

Sinnbeck's avatar

@azadi And you tried deleting it and writing it again (the blade part).

Sinnbeck's avatar

@azadi Can you try this?

@foreach ($errors->all() as $key => $error)
    <li>{{$key}} {{ $error }}</li>
@endforeach 
zxywvu's avatar

@Sinnbeck

 The name field is required.
1 The first last name field is required.
2 The mobile field is required.
3 The email field is required.
4 The password field is required.
5 The national ode field is required.
6 The state id field is required.
7 The city id field is required.
8 The address field is required.
9 The gender field is required.
Sinnbeck's avatar

@azadi Should have been

@foreach ($errors->getMessages() as $key => $error)
    <li>{{$key}} {{ $error[0] }}</li>
@endforeach  
Sinnbeck's avatar

Or just do this to see the keys

@foreach ($errors->getMessages() as $key => $error)
    <li>{{$key}} </li>
@endforeach  
zxywvu's avatar

@Sinnbeck

name The name field is required.
first_last_name The first last name field is required.
mobile The mobile field is required.
email The email field is required.
password The password field is required.
national_ode The national ode field is required.
state_id The state id field is required.
city_id The city id field is required.
address The address field is required.
gender The gender field is required.
zxywvu's avatar

@Sinnbeck

name
first_last_name
mobile
email
password
national_ode
state_id
city_id
address
gender 
Sinnbeck's avatar

@azadi Then I can only say, you are doing something wrong in your code that I cannot see. Maybe try renaming the field to something else? sex perhaps

Sinnbeck's avatar

Or put the code on github, so people can see the whole picture

MichalOravec's avatar

Oxbir, you even have a problem with just plain html, unbelievable...

Snapey's avatar

@azadi you tell us. We can't see any problem, and you are too lazy to describe the problem, then also say ' this code is not problem'

Snapey's avatar

@azadi so put some effort into your question. Tell us what the issue is, and what you have done to try and resolve the issue

Snapey's avatar

The only reason I can think that the rules will not be recognised is if you have a non-visible character in the rules array, since you have already proved that the rules array is being checked, and that when no radio is selected, the gender field is missing from the data being validated.

or

The error is being caught but is not displayed - again because of a mismatch in the names between the validation rule and the @error view.

I would delete 'gender' from each place its used (including the quotes) and retype at each place.

Snapey's avatar

@Sinnbeck Confusing.

invalid-feedback is changed to block visibility when preceded by an element with .is-invalid class.

.is-invalid~.invalid-feedback
{
    display: block;
}

Like you say, 20 seconds with the developer tools would have confirmed the issue.

Please or to participate in this conversation.