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

kaylo's avatar
Level 2

Submit a default value if a checkbox is unchecked

Hi,

I did create a form. In that form, I have one checkbox. When it is check, it's submit a value (1) to the server. This checkbox is not mandatory.

When I submit the form, if the checkbox is unchecked, I have an error : "Column 'xxxx' cannot be null.

I would like to know if it possible, in the validation rules or in the controller to assign the value 0 by default if the checkbox is unchecked. I don't want the column to be null.

I already tried to add a default value to the column in the migration file. It's not working as I expect.

Thanks for your help,

0 likes
9 replies
charlieBrown's avatar

You should leave us with a little bit of code. We can't help you without any code.

kaylo's avatar
Level 2

For sure,

this is my form (part of it)

        <form role="form" method="POST" action="/manage/products">
            {{ csrf_field() }}


            <div class="form-group">
                <label for="name">Product name</label>
                <input id="name" type="text" name="name" value="{{ old('name') }}" required> 
            </div>

            <div class="form-group">
                <label for="featured" class="checkbox">
                    <input type="checkbox" name="featured" id="featured" value="1">
                    Featured
                </label> 
            </div>                    

            <input type="submit" value="Create">

        </form>

the function in the controller


    public function store(Request $request)
    {

        $this->validate(request(), [

            'name' => 'required|string|max:255',
            'featured' => 'boolean',
            'status' => 'required|boolean',
            'description' => 'string',
            'price' => 'required|numeric',
            'weight' => 'nullable|numeric'           

        ]);

        Product::create([

            'name' => request('name'),
            'featured' => request('featured'),
            'status' => request('status'),
            'description' => request('description'),
            'price' => request('price'),
            'weight' => request('weight')

        ]);

        // return redirect('');

    }

charlieBrown's avatar

I'm guessing the status is the checkbox. You should:

'status' => 'required|boolean|nullable'

and after it you should check if it's null.

if($request->get('status') == null){
    $status = 0;
}

Then just save the model as you are doing:

'status' => $status
2 likes
kaylo's avatar
Level 2

Featured is the checkbox. But thanks to your solution, It is working now

        $this->validate(request(), [

            'name' => 'required|string|max:255',
            'featured' => 'boolean',
            'status' => 'required|boolean',
            'description' => 'string',
            'price' => 'required|numeric',
            'weight' => 'nullable|numeric'           

        ]);

        if($request->get('featured') == null){
            $featured = 0;
        }
        else{
            $featured = request('featured');
        }

        Product::create([

            'name' => request('name'),
            'featured' => $featured,
            'status' => request('status'),
            'description' => request('description'),
            'price' => request('price'),
            'weight' => request('weight')

        ]);
charlieBrown's avatar

If it solved your problem accept the asnwer. It may help others.

Cronix's avatar
Cronix
Best Answer
Level 67

If you're using php7+, you could also simplify that a bit with the null coalesce operator.

if($request->get('featured') == null){
  $featured = 0;
} else {
  $featured = request('featured');
}

becomes just $featured = $request->get('featured') ?? 0;

2 likes
andreich1980's avatar

Try to add a hidden field with the same name and default value before the checkbox.

<input type="hidden" name="my_name" value="0">
<input type="checkbox" name="my_name">

If you check the checkbox then it will override the default value and you'll get 1. If you don't check it you'll get the default value.

8 likes

Please or to participate in this conversation.