Skip to content

Commit

Permalink
Process last deferred item in validation
Browse files Browse the repository at this point in the history
This is needed for singular types since they may be stacked up
  • Loading branch information
octoberapp committed Sep 7, 2023
1 parent 0ebeae4 commit f49276d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/Database/Traits/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ protected function getRelationValidationValue($relationName)
!$this->relationLoaded($relationName) &&
$this->hasDeferred($this->sessionKey, $relationName)
) {
$fetchMethod = $this->isRelationTypeSingular($relationName) ? 'first' : 'get';
$data = $this->$relationName()->withDeferred($this->sessionKey)->$fetchMethod();
$data = $this->$relationName()->withDeferred($this->sessionKey)->get();
}
else {
$data = $this->$relationName;
Expand Down Expand Up @@ -194,12 +193,7 @@ protected function getRelationValidationValue($relationName)
return $value;
};

// Process singular
if ($this->isRelationTypeSingular($relationName)) {
return $processValidationValue($data);
}

// Process multi
// Cast to primitive type
if ($data instanceof \Illuminate\Support\Collection) {
$data = $data->all();
}
Expand All @@ -208,6 +202,12 @@ protected function getRelationValidationValue($relationName)
return null;
}

// Process singular (last item)
if ($this->isRelationTypeSingular($relationName)) {
return $processValidationValue($data[array_key_last($data)]);
}

// Process multi
$result = [];

foreach ($data as $key => $value) {
Expand Down
8 changes: 3 additions & 5 deletions src/Parse/Syntax/SyntaxModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function defineSyntaxRelations()
}

/**
* Prepare the syntax field data for saving.
* getFormSyntaxData prepares the syntax field data for saving.
*/
public function getFormSyntaxData()
{
Expand All @@ -57,9 +57,7 @@ public function getFormSyntaxData()
continue;
}

/*
* File upload
*/
// File upload
if ($params['type'] === 'fileupload' && $this->hasRelation($field)) {
if ($this->sessionKey) {
if ($image = $this->$field()->withDeferred($this->sessionKey)->first()) {
Expand All @@ -79,7 +77,7 @@ public function getFormSyntaxData()
}

/**
* Helper to get the perfect sized image.
* getThumbForImage helper to get the perfect sized image.
*/
protected function getThumbForImage($image, $params = [])
{
Expand Down

0 comments on commit f49276d

Please sign in to comment.