Skip to content

Commit 761ee29

Browse files
committed
Present is no longer ignoring other rules when field is empty
1 parent 44c996f commit 761ee29

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/Rules/Present.php

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ class Present extends Rule
2020
*/
2121
public function check($value): bool
2222
{
23+
$this->setAttributeAsRequired();
24+
2325
return $this->validation->hasValue($this->attribute->getKey());
2426
}
27+
28+
/**
29+
* Set attribute is required if $this->attribute is set
30+
*
31+
* @return void
32+
*/
33+
protected function setAttributeAsRequired()
34+
{
35+
if ($this->attribute) {
36+
$this->attribute->setRequired(true);
37+
}
38+
}
2539
}

src/Rules/Required.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function check($value): bool
3838
}
3939

4040
/**
41-
* Set attribute is required if $this->attribute is true
41+
* Set attribute is required if $this->attribute is set
4242
*
4343
* @return void
4444
*/

tests/ValidatorTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,22 @@ public function testIgnoreNextRulesWhenImplicitRulesFails()
681681
$this->assertNull($errors->first('must_accepted_field:min'));
682682
}
683683

684+
public function testNextRulesAppliedWhenEmptyValueWithPresent()
685+
{
686+
$validation = $this->validator->validate([
687+
'must_present_field' => '',
688+
], [
689+
'must_present_field' => 'present|array',
690+
]);
691+
692+
$errors = $validation->errors();
693+
694+
$this->assertEquals($errors->count(), 1);
695+
696+
$this->assertNull($errors->first('must_present_field:present'));
697+
$this->assertNotNull($errors->first('must_present_field:array'));
698+
}
699+
684700
public function testIgnoreOtherRulesWhenAttributeIsNotRequired()
685701
{
686702
$validation = $this->validator->validate([

0 commit comments

Comments
 (0)