From e2af15fcbbee1b3022cf77cc97e32eb2e2e0ce5d Mon Sep 17 00:00:00 2001 From: Jamiel Sharief <20553479+jamielsharief@users.noreply.github.com> Date: Mon, 24 May 2021 12:19:56 +0200 Subject: [PATCH] Fixed Record::validates behaviour so that fields can be invalidated before validate is called like Models. --- CHANGELOG.md | 5 +++-- src/Model/Record.php | 3 +-- tests/TestCase/Model/RecordTest.php | 31 ++++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27127a6b..072d3bed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,11 @@ # Changelog -## [3.24.3] - 2021-05-24 +## [3.24.4] - 2021-05-24 ### Fixed -- Fixed Record::patch behavior, +- Fixed Record::patch behaviour this marked every field as dirty +- Fixed Record::validates behaviour so that fields can be invalidated before validate is called like Models. ## [3.24.3] - 2021-05-22 diff --git a/src/Model/Record.php b/src/Model/Record.php index cbf35385..d349cedc 100644 --- a/src/Model/Record.php +++ b/src/Model/Record.php @@ -283,7 +283,6 @@ public function validate(string $field, $name, array $options = []): void */ public function validates(bool $isNewRecord = true): bool { - $errors = []; if ($this->dispatchCallback('beforeValidate')) { $errors = $this->validator()->validate( $this->toArray(), @@ -299,7 +298,7 @@ public function validates(bool $isNewRecord = true): bool $this->dispatchCallback('afterValidate'); } - return empty($errors); + return $this->hasErrors() === false; } /** diff --git a/tests/TestCase/Model/RecordTest.php b/tests/TestCase/Model/RecordTest.php index 830fd992..397a6fdb 100644 --- a/tests/TestCase/Model/RecordTest.php +++ b/tests/TestCase/Model/RecordTest.php @@ -115,16 +115,33 @@ public function testDefaultValuesAreSet() $this->assertTrue($form->agreeToTerms); } - public function testValidation() + public function testValidationFail() { $form = new CheckoutForm(); $form->name = 'joe'; $form->email = 'invalid-email'; $this->assertFalse($form->validates()); + } + + public function testValidation() + { + $form = new CheckoutForm(); + $form->name = 'joe'; $form->email = 'demo@example.com'; $this->assertTrue($form->validates()); } + public function testValidationPreset() + { + $form = new CheckoutForm(); + $form->name = 'joe'; + $form->email = 'demo@example.com'; + + $form->error('name', 'Needs to start with a capital letter'); + + $this->assertFalse($form->validates()); + } + public function testInvalidFieldType() { $this->expectException(BadMethodCallException::class); @@ -166,4 +183,16 @@ public function testPatch() $this->assertInstanceOf(Record::class, $form); $this->assertEquals(['name' => 'bar','email' => 'demo@example.com'], $form->toArray()); } + + public function testPatchDirty() + { + $form = Record::new([ + 'name' => 'foo', + 'email' => 'demo@example.com' + ], ['markClean' => true]); + + $form = Record::patch($form, ['email' => 'demo2@example.com']); + + $this->assertEquals(['email'], $form->modified()); + } }