PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 19, 2022

[FIXED] Laravel 7 Makes 'Integer' Validation message appears when the input is null even though it is not required

 February 19, 2022     codeigniter, laravel, php, validation     No comments   

Issue

Laravel pops up the integer validation message when the input is null

View

   <div class="form-group">
                            <label>Sipƫrfaqja(Metra Katror)</label>
                            <input type="number" name="area" value="{{old('area')}}"
                                   class="form-control form-control-border @error('area') is-invalid @enderror "
                                   placeholder="220">

                            @if($errors->has('area'))
                                <div style="color: red">{{ $errors->first('area') }}</div>
                            @endif
  </div>

Controller

 $rules = [

'area' => 'integer',
];

$messages = array(

       'area.integer' => 'Area should be integer value',
);

I leave the input null, but this message appears in the frontend Area should be integer value.


Solution

as in Laravel doc:

By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware are listed in the stack by the App\Http\Kernel class. Because of this, you will often need to mark your "optional" request fields as nullable if you do not want the validator to consider null values as invalid.

so, your code should look like:

$messages = array(

       'area.integer' => 'nullable|integer',
);


Answered By - OMR
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing