Skip to content

Commit 17786ca

Browse files
[11.x] Prohibited If Declined and Prohibited If Accepted validation rules (#54608)
* feat: add a prohibited_if_declined and prohibited_if_accepted validation rules * feat: add prohibited if accepted and if declined test cases * chore: translation, prohibited if declined and prohibited if accepted * chore: add prohibited if accepted and if declined test cases * chore: add prohibited if accepted and if declined test cases * Update validation.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 7d1f2df commit 17786ca

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

src/Illuminate/Translation/lang/en/validation.php

+2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@
131131
'present_with_all' => 'The :attribute field must be present when :values are present.',
132132
'prohibited' => 'The :attribute field is prohibited.',
133133
'prohibited_if' => 'The :attribute field is prohibited when :other is :value.',
134+
'prohibited_if_accepted' => 'The :attribute field is prohibited when :other is accepted.',
135+
'prohibited_if_declined' => 'The :attribute field is prohibited when :other is declined.',
134136
'prohibited_unless' => 'The :attribute field is prohibited unless :other is in :values.',
135137
'prohibits' => 'The :attribute field prohibits :other from being present.',
136138
'regex' => 'The :attribute field format is invalid.',

src/Illuminate/Validation/Concerns/ReplacesAttributes.php

+32
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,38 @@ protected function replaceProhibitedIf($message, $attribute, $rule, $parameters)
665665
return str_replace([':other', ':value'], $parameters, $message);
666666
}
667667

668+
/**
669+
* Replace all place-holders for the prohibited_if_accepted rule.
670+
*
671+
* @param string $message
672+
* @param string $attribute
673+
* @param string $rule
674+
* @param array<int,string> $parameters
675+
* @return string
676+
*/
677+
protected function replaceProhibitedIfAccepted($message, $attribute, $rule, $parameters)
678+
{
679+
$parameters[0] = $this->getDisplayableAttribute($parameters[0]);
680+
681+
return str_replace([':other'], $parameters, $message);
682+
}
683+
684+
/**
685+
* Replace all place-holders for the prohibited_if_declined rule.
686+
*
687+
* @param string $message
688+
* @param string $attribute
689+
* @param string $rule
690+
* @param array<int,string> $parameters
691+
* @return string
692+
*/
693+
public function replaceProhibitedIfDeclined($message, $attribute, $rule, $parameters)
694+
{
695+
$parameters[0] = $this->getDisplayableAttribute($parameters[0]);
696+
697+
return str_replace([':other'], $parameters, $message);
698+
}
699+
668700
/**
669701
* Replace all place-holders for the prohibited_unless rule.
670702
*

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

+38
Original file line numberDiff line numberDiff line change
@@ -2082,6 +2082,44 @@ public function validateProhibitedIf($attribute, $value, $parameters)
20822082
return true;
20832083
}
20842084

2085+
/**
2086+
* Validate that an attribute does not exist when another attribute was "accepted".
2087+
*
2088+
* @param string $attribute
2089+
* @param mixed $value
2090+
* @param mixed $parameters
2091+
* @return bool
2092+
*/
2093+
public function validateProhibitedIfAccepted($attribute, $value, $parameters)
2094+
{
2095+
$this->requireParameterCount(1, $parameters, 'prohibited_if_accepted');
2096+
2097+
if ($this->validateAccepted($parameters[0], $this->getValue($parameters[0]))) {
2098+
return $this->validateProhibited($attribute, $value);
2099+
}
2100+
2101+
return true;
2102+
}
2103+
2104+
/**
2105+
* Validate that an attribute does not exist when another attribute was "declined".
2106+
*
2107+
* @param string $attribute
2108+
* @param mixed $value
2109+
* @param mixed $parameters
2110+
* @return bool
2111+
*/
2112+
public function validateProhibitedIfDeclined($attribute, $value, $parameters)
2113+
{
2114+
$this->requireParameterCount(1, $parameters, 'prohibited_if_declined');
2115+
2116+
if ($this->validateDeclined($parameters[0], $this->getValue($parameters[0]))) {
2117+
return $this->validateProhibited($attribute, $value);
2118+
}
2119+
2120+
return true;
2121+
}
2122+
20852123
/**
20862124
* Validate that an attribute does not exist unless another attribute has a given value.
20872125
*

src/Illuminate/Validation/Validator.php

+2
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ class Validator implements ValidatorContract
266266
'PresentWithAll',
267267
'Prohibited',
268268
'ProhibitedIf',
269+
'ProhibitedIfAccepted',
270+
'ProhibitedIfDeclined',
269271
'ProhibitedUnless',
270272
'Prohibits',
271273
'MissingIf',

tests/Validation/ValidationValidatorTest.php

+68
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,74 @@ public function testProhibitedIf()
18631863
$this->assertSame('The last field is prohibited when first is jess.', $v->messages()->first('last'));
18641864
}
18651865

1866+
public function testValidateProhibitedAcceptedIf()
1867+
{
1868+
$trans = $this->getIlluminateArrayTranslator();
1869+
$v = new Validator($trans, ['foo' => 'yes', 'bar' => 'baz'], ['bar' => 'prohibited_if_accepted:foo']);
1870+
$this->assertTrue($v->fails());
1871+
1872+
$trans = $this->getIlluminateArrayTranslator();
1873+
$v = new Validator($trans, ['foo' => 'on', 'bar' => ''], ['bar' => 'prohibited_if_accepted:foo']);
1874+
$this->assertTrue($v->passes());
1875+
1876+
$trans = $this->getIlluminateArrayTranslator();
1877+
$v = new Validator($trans, ['foo' => '1', 'bar' => false], ['bar' => 'prohibited_if_accepted:foo']);
1878+
$this->assertTrue($v->fails());
1879+
1880+
$trans = $this->getIlluminateArrayTranslator();
1881+
$v = new Validator($trans, ['foo' => 1, 'bar' => null], ['bar' => 'prohibited_if_accepted:foo']);
1882+
$this->assertTrue($v->passes());
1883+
1884+
$trans = $this->getIlluminateArrayTranslator();
1885+
$v = new Validator($trans, ['foo' => 'true', 'bar' => ['baz']], ['bar' => 'prohibited_if_accepted:foo']);
1886+
$this->assertTrue($v->fails());
1887+
1888+
$trans = $this->getIlluminateArrayTranslator();
1889+
$v = new Validator($trans, ['foo' => true], ['bar' => 'prohibited_if_accepted:foo']);
1890+
$this->assertTrue($v->passes());
1891+
1892+
// error message
1893+
$trans = $this->getIlluminateArrayTranslator();
1894+
$trans->addLines(['validation.prohibited_if_accepted' => 'The :attribute field is prohibited when :other is accepted.'], 'en');
1895+
$v = new Validator($trans, ['foo' => 'true', 'bar' => 'baz'], ['bar' => 'prohibited_if_accepted:foo']);
1896+
$this->assertFalse($v->passes());
1897+
$this->assertSame('The bar field is prohibited when foo is accepted.', $v->messages()->first('bar'));
1898+
}
1899+
1900+
public function testValidateProhibitedDeclinedIf()
1901+
{
1902+
$trans = $this->getIlluminateArrayTranslator();
1903+
$v = new Validator($trans, ['foo' => 'no', 'bar' => 'baz'], ['bar' => 'prohibited_if_declined:foo']);
1904+
$this->assertTrue($v->fails());
1905+
1906+
$trans = $this->getIlluminateArrayTranslator();
1907+
$v = new Validator($trans, ['foo' => 'off', 'bar' => ''], ['bar' => 'prohibited_if_declined:foo']);
1908+
$this->assertTrue($v->passes());
1909+
1910+
$trans = $this->getIlluminateArrayTranslator();
1911+
$v = new Validator($trans, ['foo' => '0', 'bar' => false], ['bar' => 'prohibited_if_declined:foo']);
1912+
$this->assertTrue($v->fails());
1913+
1914+
$trans = $this->getIlluminateArrayTranslator();
1915+
$v = new Validator($trans, ['foo' => 0, 'bar' => null], ['bar' => 'prohibited_if_declined:foo']);
1916+
$this->assertTrue($v->passes());
1917+
1918+
$trans = $this->getIlluminateArrayTranslator();
1919+
$v = new Validator($trans, ['foo' => 'false', 'bar' => ['baz']], ['bar' => 'prohibited_if_declined:foo']);
1920+
$this->assertTrue($v->fails());
1921+
1922+
$trans = $this->getIlluminateArrayTranslator();
1923+
$v = new Validator($trans, ['foo' => false], ['bar' => 'prohibited_if_declined:foo']);
1924+
$this->assertTrue($v->passes());
1925+
1926+
// error message
1927+
$trans = $this->getIlluminateArrayTranslator();
1928+
$trans->addLines(['validation.prohibited_if_declined' => 'The :attribute field is prohibited when :other is declined.'], 'en');
1929+
$v = new Validator($trans, ['foo' => 'false', 'bar' => 'baz'], ['bar' => 'prohibited_if_declined:foo']);
1930+
$this->assertFalse($v->passes());
1931+
$this->assertSame('The bar field is prohibited when foo is declined.', $v->messages()->first('bar'));
1932+
}
1933+
18661934
public function testProhibitedUnless()
18671935
{
18681936
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)