Skip to content

Commit

Permalink
Fixed Record::validates behaviour so that fields can be invalidated b…
Browse files Browse the repository at this point in the history
…efore validate is called like Models.
  • Loading branch information
jamielsharief committed May 24, 2021
1 parent e1bcece commit e2af15f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 1 addition & 2 deletions src/Model/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -299,7 +298,7 @@ public function validates(bool $isNewRecord = true): bool
$this->dispatchCallback('afterValidate');
}

return empty($errors);
return $this->hasErrors() === false;
}

/**
Expand Down
31 changes: 30 additions & 1 deletion tests/TestCase/Model/RecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
}

0 comments on commit e2af15f

Please sign in to comment.