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

Laracast13's avatar

Laravel integer validate

Hello Using integer field

        Schema::create('user_groups', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique(); 
            $table->integer('allow_product_read')->default(0); 
            $table->integer('allow_product_create')->default(0); 
            $table->integer('allow_product_update')->default(0); 
            $table->integer('allow_product_delete')->default(0);          
            $table->timestamps();
        });
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255|unique:user_groups',
            'allow_lead_read'=> 'integer|max:11',
            'allow_lead_create'=> 'integer|max:11',
            'allow_lead_update'=> 'integer|max:11',
            'allow_lead_delete'=> 'integer|max:11', 
        ]);

if I check all field it works, but if one of value is not check getting error. my goal is that, if one of value is not check it will be 0

              <div class="form-group">
                <div class="form-check">
                  <input  id="allow_lead_read" name="allow_lead_read" class="form-check-input" value="1" type="checkbox">
                  <label class="form-check-label">Allow Lead Read</label>
                </div>
                <div class="form-check">
                  <input  id="allow_lead_create" name="allow_lead_create" class="form-check-input" value="1" type="checkbox">
                  <label class="form-check-label">Allow Lead Create</label>
                </div>
                <div class="form-check">
                  <input  id="allow_lead_update" name="allow_lead_update" class="form-check-input" value="1" type="checkbox">
                  <label class="form-check-label">Allow Lead Update</label>
                </div>
                <div class="form-check">
                  <input  id="allow_lead_delete" name="allow_lead_delete" class="form-check-input" value="1" type="checkbox">
                  <label class="form-check-label">Allow Lead Delete</label>
                </div>
              </div>
0 likes
5 replies
DcDev's avatar

Firstly, if you want to save checkbox fields to DB, you can use boolean laravel migration field type to save true if checkbox is checked or even tinyInteger laravel migration fields type to save 1 if checkbox is checked.

After that you want to change validation to boolean (https://laravel.com/docs/8.x/validation#rule-boolean).

Then, to set 0 if it is not checked, you can add ->default(0) to your migration fields (https://laravel.com/docs/8.x/migrations#column-modifiers), this way, if the checkbox is not checked, the default value (0) will be set for it in DB;

orest's avatar

@www888 make sure that you send 0 when it is not checked from Vue.

<input  id="allow_lead_update" name="allow_lead_update" class="form-check-input" value="1" type="checkbox" :ref="allowedLeadUpdate>

Now that you have a ref of the input checkbox. Before you send a request to the back end, check if that checkbox is checked otherwise send 0 to the back end

The other solution is to use the prepareForValidation method in the FormRequest, and check if the checkbox is missing from the request, if it is missing then set the value to 0.

DcDev's avatar

@www888 Please provide full code of store method. And full error message text.

jlrdw's avatar

@www888 if not checked nothing is passed in request for a checkbox, you have to see if nothing was passed or not, example:

$adopted = Request::has('adopted') ? 1 : 0;

In your request see if allow_lead_update was passed, etc.

Please or to participate in this conversation.