Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add unfilled_if & unfilled_with validation rules to Validator #34425

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Illuminate/Validation/Concerns/ReplacesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,32 @@ protected function replaceStartsWith($message, $attribute, $rule, $parameters)

return str_replace(':values', implode(', ', $parameters), $message);
}

/**
* Replace all place-holders for the unfilled_if rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
protected function replaceUnfilledIf($message, $attribute, $rule, $parameters)
{
return $this->replaceRequiredIf($message, $attribute, $rule, $parameters);
}

/**
* Replace all place-holders for the unfilled_with rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
protected function replaceUnfilledWith($message, $attribute, $rule, $parameters)
{
return $this->replaceRequiredWith($message, $attribute, $rule, $parameters);
}
}
40 changes: 40 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,46 @@ public function validateTimezone($attribute, $value)
return in_array($value, timezone_identifiers_list(), true);
}

/**
* Validate that an attribute is unfilled when another attribute has a given value.
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
public function validateUnfilledIf($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'unfilled_if');

[$values, $other] = $this->prepareValuesAndOther($parameters);

if (in_array($other, $values)) {
return ! $this->validateRequired($attribute, $value);
}

return true;
}

/**
* Validate that an attribute is unfilled when any other attribute exists.
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
public function validateUnfilledWith($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'unfilled_with');

if (! $this->allFailingRequired($parameters)) {
return ! $this->validateRequired($attribute, $value);
}

return true;
}

/**
* Validate that an attribute is a valid URL.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class Validator implements ValidatorContract
'RequiredWithout',
'RequiredWithoutAll',
'Same',
'UnfilledIf',
'Unique',
];

Expand Down
74 changes: 74 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5571,6 +5571,80 @@ public function testValidateFailsWithAsterisksAsDataKeys()
$this->assertSame(['data.1.date' => ['validation.date'], 'data.*.date' => ['validation.date']], $validator->messages()->toArray());
}

public function testValidateUnfilledIf()
{
$basicUnfilledRules = [
'mode' => 'required',
'full_name' => 'unfilled_if:mode,split',
'first_name' => 'required_if:mode,split',
'last_name' => 'required_if:mode,split',
];
$v = new Validator($this->getIlluminateArrayTranslator(), ['mode' => 'none'], $basicUnfilledRules);
$this->assertTrue($v->passes());

$v = new Validator($this->getIlluminateArrayTranslator(), [
'mode' => 'split',
'first_name' => 'Richard',
'last_name' => 'Bobby',
], $basicUnfilledRules);
$this->assertTrue($v->passes());

$v = new Validator($this->getIlluminateArrayTranslator(), [
'mode' => 'split',
'full_name' => 'Ricky Bobby',
'first_name' => 'Richard',
'last_name' => 'Bobby',
], $basicUnfilledRules);
$this->assertTrue($v->fails());

$v = new Validator($this->getIlluminateArrayTranslator(), [
'mode' => 'split',
'full_name' => 'Ricky Bobby',
'last_name' => 'Bobby',
], $basicUnfilledRules);
$this->assertTrue($v->fails());

$basicUnfilledRules = [
'mode' => 'required',
'full_name' => 'required_if:mode,single',
'first_name' => 'unfilled_if:mode,single',
'last_name' => 'unfilled_if:mode,single',
];
$v = new Validator($this->getIlluminateArrayTranslator(), [
'mode' => 'single',
'full_name' => 'Ricky Bobby',
], $basicUnfilledRules);
$this->assertTrue($v->passes());

$v = new Validator($this->getIlluminateArrayTranslator(), [
'mode' => 'single',
'full_name' => 'Ricky Bobby',
'last_name' => 'Bobby',
], $basicUnfilledRules);
$this->assertTrue($v->fails());
}

public function testValidateUnfilledWith()
{
$v = new Validator($this->getIlluminateArrayTranslator(), [], ['name' => 'unfilled_with:first_name', 'first_name' => 'sometimes']);
$this->assertTrue($v->passes());

$v = new Validator($this->getIlluminateArrayTranslator(), ['name' => 'Ricky Bobby'], ['name' => 'unfilled_with:first_name,last_name', 'first_name' => 'sometimes']);
$this->assertTrue($v->passes());

$v = new Validator($this->getIlluminateArrayTranslator(), ['name' => 'Ricky Bobby', 'first_name' => 'Richard', 'last_name' => 'Robertson'], ['name' => 'unfilled_with:first_name,last_name', 'first_name' => 'sometimes', 'last_name' => 'sometimes']);
$this->assertTrue($v->fails());

$v = new Validator($this->getIlluminateArrayTranslator(), ['name' => 'Ricky Bobby', 'last_name' => 'Robertson'], ['name' => 'unfilled_with:first_name,last_name', 'first_name' => 'sometimes']);
$this->assertTrue($v->fails());

$v = new Validator($this->getIlluminateArrayTranslator(), ['name' => 'Ricky Bobby', 'first_name' => 'Richard', 'last_name' => 'Robert'], ['name' => 'sometimes', 'first_name' => 'unfilled_with:name', 'last_name' => 'unfilled_with:name']);
$this->assertTrue($v->fails());

$v = new Validator($this->getIlluminateArrayTranslator(), ['first_name' => 'Richard', 'last_name' => 'Robert'], ['name' => 'sometimes', 'first_name' => 'unfilled_with:name', 'last_name' => 'unfilled_with:name']);
$this->assertTrue($v->passes());
}

protected function getTranslator()
{
return m::mock(TranslatorContract::class);
Expand Down