简体   繁体   English

Laravel 可空验证规则不起作用

[英]Laravel nullable validation rule not working

I recently upgraded to laravel 5.4 (from 5.2) to make use of the nullable validation rule.我最近升级到 laravel 5.4(从 5.2 开始)以使用可为nullable的验证规则。

I have a field act_post_code which can be either an integer OR null .我有一个字段act_post_code ,它可以是integernull So I provided the following rule in my Request class.所以我在我的Request class 中提供了以下规则。

 'act_post_code' => 'integer|nullable'

In Postman using form-data , I provide a key = act_post_code with its value = null .在 Postman 中使用form-data ,我提供了一个 key = act_post_code ,其值 = null

The response I get is the following:我得到的响应如下:

 { "act_post_code": [ "The act post code must be an integer." ] }

Explanation:解释:

Unfortunately, it seems that nullable is only valid with certain other validations.不幸的是, nullable似乎只对某些其他验证有效。

For example: 'act_post_code' => 'nullable|integer' will give you the error: "validation.integer"例如: 'act_post_code' => 'nullable|integer'会给你错误: "validation.integer"

However, 'act_post_code' => 'nullable|date' works fine.但是, 'act_post_code' => 'nullable|date'工作正常。

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

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:为了验证可以是 integer 类型或可为空的字段act_post_code ,您可以尝试以下操作:

  • 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();在迁移中声明时,存在act_post_code列的表的Schema声明列如$table->integer('act_post_code')->nullable();
  • This one might just work for you to validate 'act_post_code' =>'sometimes|nullable|integer'这可能只是为您验证'act_post_code' =>'sometimes|nullable|integer'

Open your migration file and make the this field as nullable打开您的迁移文件并将此字段设置为可为空

For eg例如

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确保它存在于可填充部分的 model 文件中

protected $fillable = ['act_post_code'];

After Some Test I found that nullable rule only work if only the data that we pass really was a null data.经过一些测试后,我发现只有当我们传递的数据确实是 null 数据时,可空规则才有效。
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.在刀片文件中,我使用Form::text('counter','')作为元素来输入我的数据。

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:当我输入带有非数字值的counter数据时,它会响应错误:
    "the counter must be a number" . "the counter must be a number"
  2. When I input the counter data with a numeric value it will pass the validation test.当我输入带有数值的counter数据时,它将通过验证测试。
  3. When I not input any data to the counter it will pass the validation test.当我没有向counter输入任何数据时,它将通过验证测试。

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:所以我使用dd($request_data)手动检查数据,或者如果您使用 ajax 只需return $request_data并使用console.log("data")打印它,例如:

 $.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.并发现当输入字段被清空时,它将给出null值。

One can die and dump request parameters and check whether the actual value is null or "null" (in string).可以死转储请求参数并检查实际值是 null 还是“null”(在字符串中)。 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" 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" ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM