Do not pass 'required' on validator
Validate like below
$this->validate($request, [
'username' => 'required|unique:login',
'password' => 'between:8,20'
]);
The above validator will accept password only if they are present but should be between 8 and 20
This is what I did in my use case
case 'update':
$rules = [
'protocol_id' => 'required',
'name' => 'required|max:30|unique:tenant.trackers'.',name,' . $id,
'ip'=>'required',
'imei' => 'max:30|unique:tenant.trackers'.',imei,' . $id,
'simcard_no' => 'between:8,15|unique:tenant.trackers'.',simcard_no,' . $id,
'data_retention_period'=>'required|integer'
];
break;
Here the tracker may or may not have sim card number , if present it will be 8 to 15 characters wrong
Update
if you still want to pass hardcoded 'NULL' value then add the following in validator
$str='NULL';
$rules = [
password => 'required|not_in:'.$str,
];
sometimes
或required
。它们是互斥的。如果您只是有时使用它,则在不为空的情况下将对其进行验证。 – MarcoAurélioDeleu 17年 2月18日在18:58sometimes
如果将值作为NULL传递,也会使测试失败:/我猜,问题出在传递NULL值…… – GTMeteor 17年 2月18日,19:45