Skip to content

Commit a4678ce

Browse files
authored
Add prohibited_if and prohibited_unless validation rules (#36516)
1 parent d9a33b1 commit a4678ce

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

src/Illuminate/Validation/Concerns/ReplacesAttributes.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,46 @@ protected function replaceRequiredUnless($message, $attribute, $rule, $parameter
374374
return str_replace([':other', ':values'], [$other, implode(', ', $values)], $message);
375375
}
376376

377+
/**
378+
* Replace all place-holders for the prohibited_if rule.
379+
*
380+
* @param string $message
381+
* @param string $attribute
382+
* @param string $rule
383+
* @param array $parameters
384+
* @return string
385+
*/
386+
protected function replaceProhibitedIf($message, $attribute, $rule, $parameters)
387+
{
388+
$parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0]));
389+
390+
$parameters[0] = $this->getDisplayableAttribute($parameters[0]);
391+
392+
return str_replace([':other', ':value'], $parameters, $message);
393+
}
394+
395+
/**
396+
* Replace all place-holders for the prohibited_unless rule.
397+
*
398+
* @param string $message
399+
* @param string $attribute
400+
* @param string $rule
401+
* @param array $parameters
402+
* @return string
403+
*/
404+
protected function replaceProhibitedUnless($message, $attribute, $rule, $parameters)
405+
{
406+
$other = $this->getDisplayableAttribute($parameters[0]);
407+
408+
$values = [];
409+
410+
foreach (array_slice($parameters, 1) as $value) {
411+
$values[] = $this->getDisplayableValue($parameters[0], $value);
412+
}
413+
414+
return str_replace([':other', ':values'], [$other, implode(', ', $values)], $message);
415+
}
416+
377417
/**
378418
* Replace all place-holders for the same rule.
379419
*

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,48 @@ public function validateRequiredIf($attribute, $value, $parameters)
14341434
return true;
14351435
}
14361436

1437+
/**
1438+
* Validate that an attribute does not exist when another attribute has a given value.
1439+
*
1440+
* @param string $attribute
1441+
* @param mixed $value
1442+
* @param mixed $parameters
1443+
* @return bool
1444+
*/
1445+
public function validateProhibitedIf($attribute, $value, $parameters)
1446+
{
1447+
$this->requireParameterCount(2, $parameters, 'prohibited_if');
1448+
1449+
[$values, $other] = $this->parseDependentRuleParameters($parameters);
1450+
1451+
if (in_array($other, $values, is_bool($other))) {
1452+
return ! $this->validateRequired($attribute, $value);
1453+
}
1454+
1455+
return true;
1456+
}
1457+
1458+
/**
1459+
* Validate that an attribute does not exist unless another attribute has a given value.
1460+
*
1461+
* @param string $attribute
1462+
* @param mixed $value
1463+
* @param mixed $parameters
1464+
* @return bool
1465+
*/
1466+
public function validateProhibitedUnless($attribute, $value, $parameters)
1467+
{
1468+
$this->requireParameterCount(2, $parameters, 'prohibited_unless');
1469+
1470+
[$values, $other] = $this->parseDependentRuleParameters($parameters);
1471+
1472+
if (! in_array($other, $values, is_bool($other))) {
1473+
return ! $this->validateRequired($attribute, $value);
1474+
}
1475+
1476+
return true;
1477+
}
1478+
14371479
/**
14381480
* Indicate that an attribute should be excluded when another attribute has a given value.
14391481
*

src/Illuminate/Validation/Validator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ class Validator implements ValidatorContract
227227
'RequiredWithAll',
228228
'RequiredWithout',
229229
'RequiredWithoutAll',
230+
'ProhibitedIf',
231+
'ProhibitedUnless',
230232
'Same',
231233
'Unique',
232234
];

tests/Validation/ValidationValidatorTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,78 @@ public function testRequiredUnless()
11461146
$this->assertSame('The last field is required unless first is in taylor, sven.', $v->messages()->first('last'));
11471147
}
11481148

1149+
public function testProhibitedIf()
1150+
{
1151+
$trans = $this->getIlluminateArrayTranslator();
1152+
$v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'prohibited_if:first,taylor']);
1153+
$this->assertTrue($v->fails());
1154+
1155+
$trans = $this->getIlluminateArrayTranslator();
1156+
$v = new Validator($trans, ['first' => 'taylor'], ['last' => 'prohibited_if:first,taylor']);
1157+
$this->assertTrue($v->passes());
1158+
1159+
$trans = $this->getIlluminateArrayTranslator();
1160+
$v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'prohibited_if:first,taylor,jess']);
1161+
$this->assertTrue($v->fails());
1162+
1163+
$trans = $this->getIlluminateArrayTranslator();
1164+
$v = new Validator($trans, ['first' => 'taylor'], ['last' => 'prohibited_if:first,taylor,jess']);
1165+
$this->assertTrue($v->passes());
1166+
1167+
$trans = $this->getIlluminateArrayTranslator();
1168+
$v = new Validator($trans, ['foo' => true, 'bar' => 'baz'], ['bar' => 'prohibited_if:foo,false']);
1169+
$this->assertTrue($v->passes());
1170+
1171+
$trans = $this->getIlluminateArrayTranslator();
1172+
$v = new Validator($trans, ['foo' => true, 'bar' => 'baz'], ['bar' => 'prohibited_if:foo,true']);
1173+
$this->assertTrue($v->fails());
1174+
1175+
// error message when passed multiple values (prohibited_if:foo,bar,baz)
1176+
$trans = $this->getIlluminateArrayTranslator();
1177+
$trans->addLines(['validation.prohibited_if' => 'The :attribute field is prohibited when :other is :value.'], 'en');
1178+
$v = new Validator($trans, ['first' => 'jess', 'last' => 'archer'], ['last' => 'prohibited_if:first,taylor,jess']);
1179+
$this->assertFalse($v->passes());
1180+
$this->assertSame('The last field is prohibited when first is jess.', $v->messages()->first('last'));
1181+
}
1182+
1183+
public function testProhibitedUnless()
1184+
{
1185+
$trans = $this->getIlluminateArrayTranslator();
1186+
$v = new Validator($trans, ['first' => 'jess', 'last' => 'archer'], ['last' => 'prohibited_unless:first,taylor']);
1187+
$this->assertTrue($v->fails());
1188+
1189+
$trans = $this->getIlluminateArrayTranslator();
1190+
$v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'prohibited_unless:first,taylor']);
1191+
$this->assertTrue($v->passes());
1192+
1193+
$trans = $this->getIlluminateArrayTranslator();
1194+
$v = new Validator($trans, ['first' => 'jess'], ['last' => 'prohibited_unless:first,taylor']);
1195+
$this->assertTrue($v->passes());
1196+
1197+
$trans = $this->getIlluminateArrayTranslator();
1198+
$v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'prohibited_unless:first,taylor,jess']);
1199+
$this->assertTrue($v->passes());
1200+
1201+
$trans = $this->getIlluminateArrayTranslator();
1202+
$v = new Validator($trans, ['first' => 'jess', 'last' => 'archer'], ['last' => 'prohibited_unless:first,taylor,jess']);
1203+
$this->assertTrue($v->passes());
1204+
1205+
$trans = $this->getIlluminateArrayTranslator();
1206+
$v = new Validator($trans, ['foo' => false, 'bar' => 'baz'], ['bar' => 'prohibited_unless:foo,false']);
1207+
$this->assertTrue($v->passes());
1208+
1209+
$trans = $this->getIlluminateArrayTranslator();
1210+
$v = new Validator($trans, ['foo' => false, 'bar' => 'baz'], ['bar' => 'prohibited_unless:foo,true']);
1211+
$this->assertTrue($v->fails());
1212+
1213+
// error message when passed multiple values (prohibited_unless:foo,bar,baz)
1214+
$trans = $this->getIlluminateArrayTranslator();
1215+
$trans->addLines(['validation.prohibited_unless' => 'The :attribute field is prohibited unless :other is in :values.'], 'en');
1216+
$v = new Validator($trans, ['first' => 'tim', 'last' => 'macdonald'], ['last' => 'prohibitedUnless:first,taylor,jess']);
1217+
$this->assertFalse($v->passes());
1218+
$this->assertSame('The last field is prohibited unless first is in taylor, jess.', $v->messages()->first('last'));
1219+
}
1220+
11491221
public function testFailedFileUploads()
11501222
{
11511223
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)