Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
validate checkbox in laravel

How to validate checkbox in laravel ?

Aman Jain, January 26, 2024March 16, 2024

Certainly! Below is a step-by-step guide on how to validate checkbox in Laravel application. Let’s assume you are working on a form that includes checkbox and you want to make sure that at least one checkbox is selected before the form is submitted.

We will take a simple example as well to understand this. Checkbox values can be 0, 1, true, false, yes and no. if we want its checked then values must be positive like 1, true and yes.

Checkbox input is check then it gives a value but if its not checked then the key for input field is unset so we do not get in the submitted form value. So to handle this we need the accepted rule of laravel validation library.

Validate checkbox in laravel using accepted rule

we will use here accepted rule

<?php 
return Validator::make(request()->all(), [
    'checkbox' =>'accepted'
]);

Let’s understand this with example

Step 1: Set up your Laravel project

If you don’t have a Laravel project set up, you can create one using Composer:

composer create-project --prefer-dist laravel/laravel checkbox-validation
cd checkbox-validation

Step 2: Create a Form

Create a form in your blade view (e.g., resources/views/checkbox-form.blade.php) with checkbox:

<form method="post" action="{{ route('process.form') }}">
    @csrf

    <label for="option1">Option 1</label>
    <input type="checkbox" id="option1" name="option" value="1">
    <button type="submit">Submit</button>
</form>

Step 3: Create a Controller

Create a controller to handle the form submission and validation:

php artisan make:controller FormController

Edit the generated FormController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FormController extends Controller
{
    public function showForm()
    {
        return view('checkbox-form');
    }

    public function processForm(Request $request)
    {
        $request->validate([
            'option' => 'accepted'
        ]);
        
        //here we get the option value if its not set then we are setting default to 0
        $valueOption = $request->get("option",'0');
        // Continue processing if validation passes

        return "Form submitted successfully!";
    }
}

here we get the option value if its not set then we are setting default to 0

$request->get("option",'0');

Step 4: Define Routes

In your routes/web.php, define the routes for showing the form and processing the form:

<?php
use App\Http\Controllers\FormController;

Route::get('/form', [FormController::class, 'showForm'])->name('show.form');
Route::post('/form', [FormController::class, 'processForm'])->name('process.form');

Step 5: Run the Application

Run the development server:

php artisan serve

Visit http://localhost:8000/form in your browser to see the form. Try submitting the form with and without selecting any checkboxe. Laravel’s validation will ensure that at least one checkbox is selected.

Related

Laravel acceptedcheckboxlaravelvalidation

Post navigation

Previous post
Next post

Related Posts

Php How to Upload image with preview in Laravel 5/6 with example

How to Upload image with preview in Laravel 5/6 with example ?

June 12, 2022June 25, 2022

In this tutorial we will learn to Upload image with preview in laravel or for any website can be basic requirement like set up a product picture or adding profile to for verification. Laravel 5/6 provides robust functionality to upload and process the image with security. Laravel simply use Request…

Read More
Php How to Add Default Value to Column in Laravel Migration

How to Add Default Value to Column in Laravel Migration?

August 17, 2022March 16, 2024

Default value to column is useful when we want to auto fill the default defined value of column during insertion of other values of table So In our this article i will show you to add default value to column in laravel migration. We can add any type of data…

Read More
Php Ajax Image Upload with form in Laravel 8 9 with example

Ajax Image Upload with form in Laravel 8 / 9 with example

June 3, 2022June 4, 2022

Ajax image Upload with preview in laravel 9 can be implement easily using the laravel file and storage providers.In a website Image uploading can be used in multiple places like set up a profile picture to providing the documents. Laravel 9 provides robust functionality to upload and process the image…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version