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

[5.3] Stop further validation if a "required" rule fails #15089

Merged
merged 1 commit into from
Aug 29, 2016
Merged
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
13 changes: 9 additions & 4 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,18 +751,23 @@ protected function validateBail()
}

/**
* Stop on error if "bail" rule is assigned and attribute has a message.
* Check if we should stop further validations on a given attribute.
*
* @param string $attribute
* @return bool
*/
protected function shouldStopValidating($attribute)
{
if (! $this->hasRule($attribute, ['Bail'])) {
return false;
if ($this->hasRule($attribute, ['Bail'])) {
return $this->messages->has($attribute);
}

return $this->messages->has($attribute);
// In case the attribute has any rule that indicates that the field is required
// and that rule already failed then we should stop validation at this point
// as now there is no point in calling other rules with this field empty.
return $this->hasRule($attribute, $this->implicitRules) &&
isset($this->failedRules[$attribute]) &&
array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,27 @@ public function testValidateFilled()
$this->assertFalse($v->passes());
}

public function testValidationStopsAtFailedPresenceCheck()
{
$trans = $this->getRealTranslator();

$v = new Validator($trans, ['name' => null], ['name' => 'Required|string']);
$v->passes();
$this->assertEquals(['validation.required'], $v->errors()->get('name'));

$v = new Validator($trans, ['name' => null, 'email' => 'email'], ['name' => 'required_with:email|string']);
$v->passes();
$this->assertEquals(['validation.required_with'], $v->errors()->get('name'));

$v = new Validator($trans, ['name' => null, 'email' => ''], ['name' => 'required_with:email|string']);
$v->passes();
$this->assertEquals(['validation.string'], $v->errors()->get('name'));

$v = new Validator($trans, [], ['name' => 'present|string']);
$v->passes();
$this->assertEquals(['validation.present'], $v->errors()->get('name'));
}

public function testValidatePresent()
{
$trans = $this->getRealTranslator();
Expand Down