Skip to content
2 changes: 2 additions & 0 deletions src/Illuminate/Translation/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@
'present_with_all' => 'The :attribute field must be present when :values are present.',
'prohibited' => 'The :attribute field is prohibited.',
'prohibited_if' => 'The :attribute field is prohibited when :other is :value.',
'prohibited_if_accepted' => 'The :attribute field is prohibited when :other is accepted.',
'prohibited_if_declined' => 'The :attribute field is prohibited when :other is declined.',
'prohibited_unless' => 'The :attribute field is prohibited unless :other is in :values.',
'prohibits' => 'The :attribute field prohibits :other from being present.',
'regex' => 'The :attribute field format is invalid.',
Expand Down
32 changes: 32 additions & 0 deletions src/Illuminate/Validation/Concerns/ReplacesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,38 @@ protected function replaceProhibitedIf($message, $attribute, $rule, $parameters)
return str_replace([':other', ':value'], $parameters, $message);
}

/**
* Replace all place-holders for the prohibited_if_accepted rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
protected function replaceProhibitedIfAccepted($message, $attribute, $rule, $parameters)
{
$parameters[0] = $this->getDisplayableAttribute($parameters[0]);

return str_replace([':other'], $parameters, $message);
}

/**
* Replace all place-holders for the prohibited_if_declined rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
public function replaceProhibitedIfDeclined($message, $attribute, $rule, $parameters)
{
$parameters[0] = $this->getDisplayableAttribute($parameters[0]);

return str_replace([':other'], $parameters, $message);
}

/**
* Replace all place-holders for the prohibited_unless rule.
*
Expand Down
38 changes: 38 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,44 @@ public function validateProhibitedIf($attribute, $value, $parameters)
return true;
}

/**
* Validate that an attribute does not exist when another attribute was "accepted".
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
public function validateProhibitedIfAccepted($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'prohibited_if_accepted');

if ($this->validateAccepted($parameters[0], $this->getValue($parameters[0]))) {
return $this->validateProhibited($attribute, $value);
}

return true;
}

/**
* Validate that an attribute does not exist when another attribute was "declined".
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
public function validateProhibitedIfDeclined($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'prohibited_if_declined');

if ($this->validateDeclined($parameters[0], $this->getValue($parameters[0]))) {
return $this->validateProhibited($attribute, $value);
}

return true;
}

/**
* Validate that an attribute does not exist unless another attribute has a given value.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ class Validator implements ValidatorContract
'PresentWithAll',
'Prohibited',
'ProhibitedIf',
'ProhibitedIfAccepted',
'ProhibitedIfDeclined',
'ProhibitedUnless',
'Prohibits',
'MissingIf',
Expand Down
68 changes: 68 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,74 @@ public function testProhibitedIf()
$this->assertSame('The last field is prohibited when first is jess.', $v->messages()->first('last'));
}

public function testValidateProhibitedAcceptedIf()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => 'yes', 'bar' => 'baz'], ['bar' => 'prohibited_if_accepted:foo']);
$this->assertTrue($v->fails());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => 'on', 'bar' => ''], ['bar' => 'prohibited_if_accepted:foo']);
$this->assertTrue($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => '1', 'bar' => false], ['bar' => 'prohibited_if_accepted:foo']);
$this->assertTrue($v->fails());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => 1, 'bar' => null], ['bar' => 'prohibited_if_accepted:foo']);
$this->assertTrue($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => 'true', 'bar' => ['baz']], ['bar' => 'prohibited_if_accepted:foo']);
$this->assertTrue($v->fails());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => true], ['bar' => 'prohibited_if_accepted:foo']);
$this->assertTrue($v->passes());

// error message
$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.prohibited_if_accepted' => 'The :attribute field is prohibited when :other is accepted.'], 'en');
$v = new Validator($trans, ['foo' => 'true', 'bar' => 'baz'], ['bar' => 'prohibited_if_accepted:foo']);
$this->assertFalse($v->passes());
$this->assertSame('The bar field is prohibited when foo is accepted.', $v->messages()->first('bar'));
}

public function testValidateProhibitedDeclinedIf()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => 'no', 'bar' => 'baz'], ['bar' => 'prohibited_if_declined:foo']);
$this->assertTrue($v->fails());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => 'off', 'bar' => ''], ['bar' => 'prohibited_if_declined:foo']);
$this->assertTrue($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => '0', 'bar' => false], ['bar' => 'prohibited_if_declined:foo']);
$this->assertTrue($v->fails());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => 0, 'bar' => null], ['bar' => 'prohibited_if_declined:foo']);
$this->assertTrue($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => 'false', 'bar' => ['baz']], ['bar' => 'prohibited_if_declined:foo']);
$this->assertTrue($v->fails());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => false], ['bar' => 'prohibited_if_declined:foo']);
$this->assertTrue($v->passes());

// error message
$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.prohibited_if_declined' => 'The :attribute field is prohibited when :other is declined.'], 'en');
$v = new Validator($trans, ['foo' => 'false', 'bar' => 'baz'], ['bar' => 'prohibited_if_declined:foo']);
$this->assertFalse($v->passes());
$this->assertSame('The bar field is prohibited when foo is declined.', $v->messages()->first('bar'));
}

public function testProhibitedUnless()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down
Loading