[英]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
,它可以是integer
或null
。 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." ] }
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'
工作正常。
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
,您可以尝试以下操作:
act_post_code
declare the column like $table->integer('act_post_code')->nullable();
act_post_code
列的表的Schema声明列如$table->integer('act_post_code')->nullable();
'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:然后我将它与几个测试用例一起使用:
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"
。counter
data with a numeric value it will pass the validation test.counter
数据时,它将通过验证测试。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.