Laravel nullable validation rule not working
Asked Answered
L

5

6

I recently upgraded to laravel 5.4 (from 5.2) to make use of the nullable validation rule.

I have a field act_post_code which can be either an integer OR null. So I provided the following rule in my Request class.

'act_post_code' => 'integer|nullable'

In Postman using form-data, I provide a key = act_post_code with its value = null.

The response I get is the following:

{
    "act_post_code": [
        "The act post code must be an integer."
    ]
}
Lubber answered 7/2, 2018 at 23:26 Comment(10)
Use nullable before integer in the rule list.Monolatry
@btl Didn't work. Same response.Lubber
Is act_post_code nullable in your table definition?Monolatry
I believe so. act_post_code varchar(4) DEFAULT NULL,Lubber
Please update your question to include the full contents of the validation portion of your controller, and the contents of your form, so that we can identify any mismatch in field names as this is indicative of that sort of error -- assuming you have indeed correctly updated to Laravel 5.4.Madalena
@Madalena if I enter an integer in postman it works, so I don't think there is a mismatch of names. Do you know if there is anything in particular from the upgrade that causes this?Lubber
what happens if you try with nullable onlyStraphanger
@Straphanger it then accepts null ... hmm so it might be the way postman interprets null, its being taken as a string?Lubber
no, laravel has middleware that convert empty strings to null. your code looks good, what are you putting in postmanStraphanger
Im just putting in null, with no quotes around it.Lubber
C
7

Explanation:

Unfortunately, it seems that nullable is only valid with certain other validations.

For example: 'act_post_code' => 'nullable|integer' will give you the error: "validation.integer"

However, 'act_post_code' => 'nullable|date' works fine.

Fix:

As a work around for these validations, you can make them dynamic. For example, before the validator:

$act_post_code_rules = $request->act_post_code ? 'integer' : '';

then, within the validate:

'act_post_code' => $act_post_code_rules

Commines answered 28/2, 2020 at 16:56 Comment(1)
I see this described behaviour in laravel 8.8 - only certain combinations like nullable and integer do not work. Imo it's a bug.Annoy
S
2

In order to validate the field act_post_code which can be either be of type integer or nullable, you can try out the following :

  • When declaring in the migration ,the Schema of the table where there is the column act_post_code declare the column like $table->integer('act_post_code')->nullable();
  • This one might just work for you to validate 'act_post_code' =>'sometimes|nullable|integer'
Spoon answered 14/2, 2018 at 23:41 Comment(2)
Take a look at : laravel docs and [this] (laravel-guide.readthedocs.io/en/latest/validation) and search for nullable and sometimes!Custos
2'nd option not working for Laravel 8.8Annoy
M
2

One can die and dump request parameters and check whether the actual value is null or "null" (in string). Sometimes when submitting a form via javascript we use FormData() to append data to the form, in those scenarios it may send a null value as in string type "null"

array:5 [
  "firstName" => "Kaustubh"
  "middleName" => "null" // null as string
  "lastName" => "Bagwe"
  "contactNumber" => null // null value
  "photo" => null
  "_method" => "PUT"
]
Malda answered 4/7, 2022 at 7:11 Comment(0)
F
1

Open your migration file and make the this field as nullable

For e.g

Schema::create('your_table_name', function (Blueprint $table) {
    $table->integer('act_post_code ')->nullable();    
});

Make sure it is present in your model file in the fillable section

protected $fillable = ['act_post_code'];
Firstnighter answered 8/2, 2018 at 7:12 Comment(0)
L
1

After Some Test I found that nullable rule only work if only the data that we pass really was a null data.
so in my test case i use the validation rule like this :
"counter" => "nullable|numeric"
and in the blade file, I use Form::text('counter','') as element to input my data.

Then i use it with few test case:

  1. When I input the counter data with a non-numeric value it will response with error:
    "the counter must be a number".
  2. When I input the counter data with a numeric value it will pass the validation test.
  3. When I not input any data to the counter it will pass the validation test.

so i check the data manually using dd($request_data)or if you using ajax just return $request_data and print it using console.log("data") like:

$.ajax({
type:'POST',
data:{_token:"{{ csrf_token() }}",
    counter:$('input[name="counter"]').val()
},success:function(data){
  console.log(data);   
 }
});

and found out that when input field is emptied it will give the null value.

Lenient answered 22/12, 2019 at 4:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.