Skip to content

Commit

Permalink
Fix Field mandatory, check in normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Oct 4, 2021
1 parent 09187a5 commit 1bc8d76
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function __construct()
$value = $persistence->typecastLoadField($this, $value);

if ($value === null) {
if ($this->required/* known bug, see https://github.com/atk4/data/issues/575, fix in https://github.com/atk4/data/issues/576 || $this->mandatory*/) {
if ($this->required || $this->mandatory) {
throw new Exception('Must not be null');
}

Expand Down
9 changes: 1 addition & 8 deletions src/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,7 @@ public function typecastSaveRow(Model $model, array $row): array

$field = $model->getField($fieldName);

$value = $this->typecastSaveField($field, $value);

// check null values for mandatory fields
if ($value === null && $field->mandatory) {
throw new ValidationException([$field->short_name => 'Mandatory field value cannot be null'], $field->getOwner());
}

$result[$field->getPersistenceName()] = $value;
$result[$field->getPersistenceName()] = $this->typecastSaveField($field, $value);
}

return $result;
Expand Down
5 changes: 1 addition & 4 deletions tests/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ public function testMandatory1(): void
$m->set('foo', 'abc');
$m->set('foo', '');

/* known bug, see https://github.com/atk4/data/issues/575, fix in https://github.com/atk4/data/issues/576
$this->expectException(ValidationException::class);*/
$this->expectException(ValidationException::class);
$m->set('foo', null);

$this->assertTrue(true); // no exceptions
}

public function testRequired1(): void
Expand Down
5 changes: 2 additions & 3 deletions tests/Util/DeepCopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,15 @@ public function testDeepError(): void
$dc = new DeepCopy();

$this->expectException(DeepCopyException::class);

try {
$invoice = $dc
->from($quote)
->excluding(['Lines' => ['qty']])
->to($invoice)
->with(['Lines'])
->copy();
} catch (\Atk4\Data\Util\DeepCopyException $e) {
$this->assertSame('Mandatory field value cannot be null', $e->getPrevious()->getMessage());
} catch (DeepCopyException $e) {
$this->assertSame('Must not be null', $e->getPrevious()->getMessage());

throw $e;
}
Expand Down

0 comments on commit 1bc8d76

Please sign in to comment.