Skip to content

Commit 88f8b02

Browse files
authored
[9.x] Add RequiredIfAccepted validation rule (#44035)
* RequiredIfAccepted validation rule * RequiredIfAccepted attribute placeholder replacer + styling fixes * Update ValidatesAttributes.php
1 parent fc9dc59 commit 88f8b02

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

src/Illuminate/Validation/Concerns/ReplacesAttributes.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,22 @@ protected function replaceRequiredIf($message, $attribute, $rule, $parameters)
434434
return str_replace([':other', ':value'], $parameters, $message);
435435
}
436436

437+
/**
438+
* Replace all place-holders for the required_if_accepted rule.
439+
*
440+
* @param string $message
441+
* @param string $attribute
442+
* @param string $rule
443+
* @param array<int,string> $parameters
444+
* @return string
445+
*/
446+
protected function replaceRequiredIfAccepted($message, $attribute, $rule, $parameters)
447+
{
448+
$parameters[0] = $this->getDisplayableAttribute($parameters[0]);
449+
450+
return str_replace([':other'], $parameters, $message);
451+
}
452+
437453
/**
438454
* Replace all place-holders for the required_unless rule.
439455
*

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,25 @@ public function validateRequiredIf($attribute, $value, $parameters)
16011601
return true;
16021602
}
16031603

1604+
/**
1605+
* Validate that an attribute exists when another attribute was "accepted".
1606+
*
1607+
* @param string $attribute
1608+
* @param mixed $value
1609+
* @param mixed $parameters
1610+
* @return bool
1611+
*/
1612+
public function validateRequiredIfAccepted($attribute, $value, $parameters)
1613+
{
1614+
$this->requireParameterCount(1, $parameters, 'required_if_accepted');
1615+
1616+
if ($this->validateAccepted($parameters[0], $this->getValue($parameters[0]))) {
1617+
return $this->validateRequired($attribute, $value);
1618+
}
1619+
1620+
return true;
1621+
}
1622+
16041623
/**
16051624
* Validate that an attribute does not exist.
16061625
*

src/Illuminate/Validation/Validator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ class Validator implements ValidatorContract
210210
'Present',
211211
'Required',
212212
'RequiredIf',
213+
'RequiredIfAccepted',
213214
'RequiredUnless',
214215
'RequiredWith',
215216
'RequiredWithAll',
@@ -240,6 +241,7 @@ class Validator implements ValidatorContract
240241
'AcceptedIf',
241242
'DeclinedIf',
242243
'RequiredIf',
244+
'RequiredIfAccepted',
243245
'RequiredUnless',
244246
'RequiredWith',
245247
'RequiredWithAll',

tests/Validation/ValidationValidatorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,22 @@ public function testValidateAccepted()
19721972
$this->assertTrue($v->passes());
19731973
}
19741974

1975+
public function testValidateRequiredAcceptedIf()
1976+
{
1977+
$trans = $this->getIlluminateArrayTranslator();
1978+
$v = new Validator($trans, ['foo' => 'no', 'bar' => 'baz'], ['bar' => 'required_if_accepted:foo']);
1979+
$this->assertTrue($v->passes());
1980+
1981+
$v = new Validator($trans, ['foo' => 'yes', 'bar' => 'baz'], ['bar' => 'required_if_accepted:foo']);
1982+
$this->assertTrue($v->passes());
1983+
1984+
$v = new Validator($trans, ['foo' => 'no', 'bar' => ''], ['bar' => 'required_if_accepted:foo']);
1985+
$this->assertTrue($v->passes());
1986+
1987+
$v = new Validator($trans, ['foo' => 'yes', 'bar' => ''], ['bar' => 'required_if_accepted:foo']);
1988+
$this->assertFalse($v->passes());
1989+
}
1990+
19751991
public function testValidateAcceptedIf()
19761992
{
19771993
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)