Skip to content

Commit f02d8d5

Browse files
committed
Fix required_if on array attribute
1 parent d05b52a commit f02d8d5

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Rules/RequiredIf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function check($value)
2323

2424
$anotherAttribute = $this->parameter('field');
2525
$definedValues = $this->parameter('values');
26-
$anotherValue = $this->validation->getValue($anotherAttribute);
26+
$anotherValue = $this->getAttribute()->getValue($anotherAttribute);
2727

2828
$validator = $this->validation->getValidator();
2929
$required_validator = $validator('required');

tests/ValidatorTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,4 +808,38 @@ public function testCustomMessageInArrayValidation()
808808
$this->assertEquals($errors->first('cart.*.attributes.*.value'), 'Item 2 attribute 1 value is required');
809809
}
810810

811+
public function testRequiredIfOnArrayAttribute()
812+
{
813+
$validation = $this->validator->validate([
814+
'products' => [
815+
// invalid because has_notes is not empty
816+
'10' => [
817+
'quantity' => 8,
818+
'has_notes' => 1,
819+
'notes' => ''
820+
],
821+
// valid because has_notes is null
822+
'12' => [
823+
'quantity' => 0,
824+
'has_notes' => null,
825+
'notes' => ''
826+
],
827+
// valid because no has_notes
828+
'14' => [
829+
'quantity' => 0,
830+
'notes' => ''
831+
],
832+
]
833+
], [
834+
'products.*.notes' => 'required_if:products.*.has_notes,1',
835+
]);
836+
837+
$this->assertFalse($validation->passes());
838+
839+
$errors = $validation->errors();
840+
$this->assertNotNull($errors->first('products.10.notes'));
841+
$this->assertNull($errors->first('products.12.notes'));
842+
$this->assertNull($errors->first('products.14.notes'));
843+
}
844+
811845
}

0 commit comments

Comments
 (0)