@@ -1420,9 +1420,13 @@ public function validateRequiredIf($attribute, $value, $parameters)
1420
1420
{
1421
1421
$ this ->requireParameterCount (2 , $ parameters , 'required_if ' );
1422
1422
1423
+ if (! Arr::has ($ this ->data , $ parameters [0 ])) {
1424
+ return true ;
1425
+ }
1426
+
1423
1427
[$ values , $ other ] = $ this ->prepareValuesAndOther ($ parameters );
1424
1428
1425
- if (in_array ($ other , $ values , is_bool ($ other ))) {
1429
+ if (in_array ($ other , $ values , is_bool ($ other ) || is_null ( $ other ) )) {
1426
1430
return $ this ->validateRequired ($ attribute , $ value );
1427
1431
}
1428
1432
@@ -1441,9 +1445,13 @@ public function validateExcludeIf($attribute, $value, $parameters)
1441
1445
{
1442
1446
$ this ->requireParameterCount (2 , $ parameters , 'exclude_if ' );
1443
1447
1448
+ if (! Arr::has ($ this ->data , $ parameters [0 ])) {
1449
+ return true ;
1450
+ }
1451
+
1444
1452
[$ values , $ other ] = $ this ->prepareValuesAndOther ($ parameters );
1445
1453
1446
- return ! in_array ($ other , $ values , is_bool ($ other ));
1454
+ return ! in_array ($ other , $ values , is_bool ($ other ) || is_null ( $ other ) );
1447
1455
}
1448
1456
1449
1457
/**
@@ -1458,9 +1466,38 @@ public function validateExcludeUnless($attribute, $value, $parameters)
1458
1466
{
1459
1467
$ this ->requireParameterCount (2 , $ parameters , 'exclude_unless ' );
1460
1468
1469
+ if (! Arr::has ($ this ->data , $ parameters [0 ])) {
1470
+ return true ;
1471
+ }
1472
+
1461
1473
[$ values , $ other ] = $ this ->prepareValuesAndOther ($ parameters );
1462
1474
1463
- return in_array ($ other , $ values , is_bool ($ other ));
1475
+ return in_array ($ other , $ values , is_bool ($ other ) || is_null ($ other ));
1476
+ }
1477
+
1478
+ /**
1479
+ * Validate that an attribute exists when another attribute does not have a given value.
1480
+ *
1481
+ * @param string $attribute
1482
+ * @param mixed $value
1483
+ * @param mixed $parameters
1484
+ * @return bool
1485
+ */
1486
+ public function validateRequiredUnless ($ attribute , $ value , $ parameters )
1487
+ {
1488
+ $ this ->requireParameterCount (2 , $ parameters , 'required_unless ' );
1489
+
1490
+ if (! Arr::has ($ this ->data , $ parameters [0 ])) {
1491
+ return true ;
1492
+ }
1493
+
1494
+ [$ values , $ other ] = $ this ->prepareValuesAndOther ($ parameters );
1495
+
1496
+ if (! in_array ($ other , $ values , is_bool ($ other ) || is_null ($ other ))) {
1497
+ return $ this ->validateRequired ($ attribute , $ value );
1498
+ }
1499
+
1500
+ return true ;
1464
1501
}
1465
1502
1466
1503
/**
@@ -1479,6 +1516,10 @@ protected function prepareValuesAndOther($parameters)
1479
1516
$ values = $ this ->convertValuesToBoolean ($ values );
1480
1517
}
1481
1518
1519
+ if ($ this ->shouldConvertToNull ($ parameters [0 ]) || is_null ($ other )) {
1520
+ $ values = $ this ->convertValuesToNull ($ values );
1521
+ }
1522
+
1482
1523
return [$ values , $ other ];
1483
1524
}
1484
1525
@@ -1493,6 +1534,17 @@ protected function shouldConvertToBoolean($parameter)
1493
1534
return in_array ('boolean ' , Arr::get ($ this ->rules , $ parameter , []));
1494
1535
}
1495
1536
1537
+ /**
1538
+ * Check if parameter should be converted to null.
1539
+ *
1540
+ * @param string $parameter
1541
+ * @return bool
1542
+ */
1543
+ protected function shouldConvertToNull ($ parameter )
1544
+ {
1545
+ return in_array ('nullable ' , Arr::get ($ this ->rules , $ parameter , []));
1546
+ }
1547
+
1496
1548
/**
1497
1549
* Convert the given values to boolean if they are string "true" / "false".
1498
1550
*
@@ -1513,24 +1565,20 @@ protected function convertValuesToBoolean($values)
1513
1565
}
1514
1566
1515
1567
/**
1516
- * Validate that an attribute exists when another attribute does not have a given value .
1568
+ * Convert the given values to null if they are string "null" .
1517
1569
*
1518
- * @param string $attribute
1519
- * @param mixed $value
1520
- * @param mixed $parameters
1521
- * @return bool
1570
+ * @param array $values
1571
+ * @return array
1522
1572
*/
1523
- public function validateRequiredUnless ( $ attribute , $ value , $ parameters )
1573
+ protected function convertValuesToNull ( $ values )
1524
1574
{
1525
- $ this ->requireParameterCount (2 , $ parameters , 'required_unless ' );
1526
-
1527
- [$ values , $ other ] = $ this ->prepareValuesAndOther ($ parameters );
1528
-
1529
- if (! in_array ($ other , $ values , is_bool ($ other ))) {
1530
- return $ this ->validateRequired ($ attribute , $ value );
1531
- }
1575
+ return array_map (function ($ value ) {
1576
+ if ($ value === 'null ' ) {
1577
+ return null ;
1578
+ }
1532
1579
1533
- return true ;
1580
+ return $ value ;
1581
+ }, $ values );
1534
1582
}
1535
1583
1536
1584
/**
0 commit comments