Skip to content

Commit

Permalink
Fix multiline validation (#1658)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibelar authored Sep 14, 2021
1 parent f31b5b8 commit 6ca70bd
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/Form/Control/Multiline.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,26 +308,26 @@ public function getValue(): string
public function validate(array $rows): array
{
$rowErrors = [];
$model = $this->getModel();
$entity = $this->getModel()->createEntity();

foreach ($rows as $cols) {
$rowId = $this->getMlRowId($cols);
foreach ($cols as $fieldName => $value) {
if ($fieldName === '__atkml' || $fieldName === $model->id_field) {
if ($fieldName === '__atkml' || $fieldName === $entity->id_field) {
continue;
}

try {
$field = $model->getField($fieldName);
$field = $entity->getField($fieldName);
// Save field value only if the field was editable
if (!$field->read_only) {
$model->createEntity()->set($fieldName, $value);
$entity->set($fieldName, $value);
}
} catch (\Atk4\Core\Exception $e) {
$rowErrors[$rowId][] = ['name' => $fieldName, 'msg' => $e->getMessage()];
}
}
$rowErrors = $this->addModelValidateErrors($rowErrors, $rowId, $model);
$rowErrors = $this->addModelValidateErrors($rowErrors, $rowId, $entity);
}

return $rowErrors;
Expand Down Expand Up @@ -373,12 +373,12 @@ public function saveRows(): self
/**
* Check for model validate error.
*/
protected function addModelValidateErrors(array $errors, string $rowId, Model $model): array
protected function addModelValidateErrors(array $errors, string $rowId, Model $entity): array
{
$e = $model->validate();
if ($e) {
foreach ($e as $field => $msg) {
$errors[$rowId][] = ['field' => $field, 'msg' => $msg];
$entityErrors = $entity->validate();
if ($entityErrors) {
foreach ($entityErrors as $fieldName => $msg) {
$errors[$rowId][] = ['name' => $fieldName, 'msg' => $msg];
}
}

Expand Down

0 comments on commit 6ca70bd

Please sign in to comment.