Skip to content

Commit 6e8619a

Browse files
committed
Add prohibited_with and prohibited_without validation rules
1 parent 597f5ab commit 6e8619a

File tree

4 files changed

+380
-0
lines changed

4 files changed

+380
-0
lines changed

src/Illuminate/Validation/Concerns/ReplacesAttributes.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,62 @@ protected function replaceRequiredWithoutAll($message, $attribute, $rule, $param
248248
return $this->replaceRequiredWith($message, $attribute, $rule, $parameters);
249249
}
250250

251+
/**
252+
* Replace all place-holders for the prohibited_with rule.
253+
*
254+
* @param string $message
255+
* @param string $attribute
256+
* @param string $rule
257+
* @param array $parameters
258+
* @return string
259+
*/
260+
protected function replaceProhibitedWith($message, $attribute, $rule, $parameters)
261+
{
262+
return str_replace(':values', implode(' / ', $this->getAttributeList($parameters)), $message);
263+
}
264+
265+
/**
266+
* Replace all place-holders for the prohibited_with_all rule.
267+
*
268+
* @param string $message
269+
* @param string $attribute
270+
* @param string $rule
271+
* @param array $parameters
272+
* @return string
273+
*/
274+
protected function replaceProhibitedWithAll($message, $attribute, $rule, $parameters)
275+
{
276+
return $this->replaceProhibitedWith($message, $attribute, $rule, $parameters);
277+
}
278+
279+
/**
280+
* Replace all place-holders for the prohibited_without rule.
281+
*
282+
* @param string $message
283+
* @param string $attribute
284+
* @param string $rule
285+
* @param array $parameters
286+
* @return string
287+
*/
288+
protected function replaceProhibitedWithout($message, $attribute, $rule, $parameters)
289+
{
290+
return $this->replaceProhibitedWith($message, $attribute, $rule, $parameters);
291+
}
292+
293+
/**
294+
* Replace all place-holders for the prohibited_without_all rule.
295+
*
296+
* @param string $message
297+
* @param string $attribute
298+
* @param string $rule
299+
* @param array $parameters
300+
* @return string
301+
*/
302+
protected function replaceProhibitedWithoutAll($message, $attribute, $rule, $parameters)
303+
{
304+
return $this->replaceProhibitedWith($message, $attribute, $rule, $parameters);
305+
}
306+
251307
/**
252308
* Replace all place-holders for the size rule.
253309
*

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,58 @@ public function validateProhibitedUnless($attribute, $value, $parameters)
14891489
return true;
14901490
}
14911491

1492+
/**
1493+
* Validate that an attribute does not exist when any other attribute exists.
1494+
*
1495+
* @param string $attribute
1496+
* @param mixed $value
1497+
* @param mixed $parameters
1498+
* @return bool
1499+
*/
1500+
public function validateProhibitedWith($attribute, $value, $parameters)
1501+
{
1502+
return ! $this->anyPassingRequired($parameters);
1503+
}
1504+
1505+
/**
1506+
* Validate that an attribute does not exist when all other attributes exist.
1507+
*
1508+
* @param string $attribute
1509+
* @param mixed $value
1510+
* @param mixed $parameters
1511+
* @return bool
1512+
*/
1513+
public function validateProhibitedWithAll($attribute, $value, $parameters)
1514+
{
1515+
return ! $this->allPassingRequired($parameters);
1516+
}
1517+
1518+
/**
1519+
* Validate that an attribute does not exist when another attribute does not exist.
1520+
*
1521+
* @param string $attribute
1522+
* @param mixed $value
1523+
* @param mixed $parameters
1524+
* @return bool
1525+
*/
1526+
public function validateProhibitedWithout($attribute, $value, $parameters)
1527+
{
1528+
return ! $this->anyFailingRequired($parameters);
1529+
}
1530+
1531+
/**
1532+
* Validate that an attribute does not exist when all other attributes do not exist.
1533+
*
1534+
* @param string $attribute
1535+
* @param mixed $value
1536+
* @param mixed $parameters
1537+
* @return bool
1538+
*/
1539+
public function validateProhibitedWithoutAll($attribute, $value, $parameters)
1540+
{
1541+
return $this->anyPassingRequired($parameters);
1542+
}
1543+
14921544
/**
14931545
* Indicate that an attribute should be excluded when another attribute has a given value.
14941546
*
@@ -1729,6 +1781,40 @@ protected function allFailingRequired(array $attributes)
17291781
return true;
17301782
}
17311783

1784+
/**
1785+
* Determine if any of the given attributes pass the required test.
1786+
*
1787+
* @param array $attributes
1788+
* @return bool
1789+
*/
1790+
protected function anyPassingRequired(array $attributes)
1791+
{
1792+
foreach ($attributes as $key) {
1793+
if ($this->validateRequired($key, $this->getValue($key))) {
1794+
return true;
1795+
}
1796+
}
1797+
1798+
return false;
1799+
}
1800+
1801+
/**
1802+
* Determine if all of the given attributes pass the required test.
1803+
*
1804+
* @param array $attributes
1805+
* @return bool
1806+
*/
1807+
protected function allPassingRequired(array $attributes)
1808+
{
1809+
foreach ($attributes as $key) {
1810+
if (! $this->validateRequired($key, $this->getValue($key))) {
1811+
return false;
1812+
}
1813+
}
1814+
1815+
return true;
1816+
}
1817+
17321818
/**
17331819
* Validate that two attributes match.
17341820
*

src/Illuminate/Validation/Validator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ class Validator implements ValidatorContract
231231
'Prohibited',
232232
'ProhibitedIf',
233233
'ProhibitedUnless',
234+
'ProhibitedWith',
235+
'ProhibitedWithAll',
236+
'ProhibitedWithout',
237+
'ProhibitedWithoutAll',
234238
'Same',
235239
'Unique',
236240
];

0 commit comments

Comments
 (0)