Skip to content

Commit

Permalink
Fix validation of attributes of type TranslatedText (#839)
Browse files Browse the repository at this point in the history
* fix: validation rules to be applied on elements in `TranslatedText`

* fix: "reset" the validation rules applied to each sub-field in `TranslatedText`

* chore: add tests for validating attribute fields (required/optional) and their additional rules

---------

Co-authored-by: Alec Ritson <hello@itsalec.co.uk>
  • Loading branch information
fakeheal and alecritson authored Feb 14, 2023
1 parent 6c1ab08 commit 1a362cc
Show file tree
Hide file tree
Showing 2 changed files with 271 additions and 42 deletions.
76 changes: 44 additions & 32 deletions packages/admin/src/Http/Livewire/Traits/WithAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function mapAttributes()
/**
* Parse the attributes into the correct collection format.
*
* @param \Illuminate\Support\Collection $attributes
* @param \Illuminate\Support\Collection $attributes
* @return \Illuminate\Support\Collection
*/
protected function parseAttributes(Collection $attributes, $existingData, $key = 'attributeMapping')
Expand All @@ -64,7 +64,7 @@ protected function parseAttributes(Collection $attributes, $existingData, $key =
return ! class_exists($attribute->type);
})->mapWithKeys(function ($attribute) use ($key, $existingData) {
$data = $existingData ?
$existingData->first(fn ($value, $handle) => $handle == $attribute->handle)
$existingData->first(fn($value, $handle) => $handle == $attribute->handle)
: null;

$value = $data ? $data->getValue() : null;
Expand All @@ -73,24 +73,26 @@ protected function parseAttributes(Collection $attributes, $existingData, $key =
$value = $this->prepareTranslatedText($value);
}

$reference = 'a_'.$attribute->id;

return [$reference => [
'name' => $attribute->translate('name'),
'group' => $attribute->attributeGroup->translate('name'),
'group_id' => $attribute->attributeGroup->id,
'group_handle' => $attribute->attributeGroup->handle,
'group_position' => $attribute->attributeGroup->position,
'id' => $attribute->handle,
'signature' => "{$key}.{$reference}.data",
'type' => $attribute->type,
'handle' => $attribute->handle,
'configuration' => $attribute->configuration,
'required' => $attribute->required,
'view' => app()->make($attribute->type)->getView(),
'validation' => $attribute->validation_rules,
'data' => $value,
]];
$reference = 'a_' . $attribute->id;

return [
$reference => [
'name' => $attribute->translate('name'),
'group' => $attribute->attributeGroup->translate('name'),
'group_id' => $attribute->attributeGroup->id,
'group_handle' => $attribute->attributeGroup->handle,
'group_position' => $attribute->attributeGroup->position,
'id' => $attribute->handle,
'signature' => "{$key}.{$reference}.data",
'type' => $attribute->type,
'handle' => $attribute->handle,
'configuration' => $attribute->configuration,
'required' => $attribute->required,
'view' => app()->make($attribute->type)->getView(),
'validation' => $attribute->validation_rules,
'data' => $value,
]
];
});
}

Expand All @@ -103,7 +105,7 @@ public function getAttributeGroupsProperty()
->get()->map(function ($group) {
return [
'model' => $group,
'fields' => $this->attributeMapping->filter(fn ($att) => $att['group_id'] == $group->id),
'fields' => $this->attributeMapping->filter(fn($att) => $att['group_id'] == $group->id),
];
});
}
Expand Down Expand Up @@ -136,7 +138,7 @@ public function prepareAttributeData($attributes = null)
/**
* Map translated values into field types.
*
* @param array $data
* @param array $data
* @return \Lunar\FieldTypes\TranslatedText
*/
protected function mapTranslatedText($data)
Expand All @@ -152,7 +154,7 @@ protected function mapTranslatedText($data)
/**
* Prepare translated text field for Livewire modeling.
*
* @param string|array $value
* @param string|array $value
* @return array
*/
protected function prepareTranslatedText($value)
Expand Down Expand Up @@ -187,28 +189,38 @@ public function withAttributesValidationRules()
}

$validation = $attribute['validation'] ? explode(',', $attribute['validation']) : [];

$field = $attribute['signature'];

if (($attribute['required'] ?? false) || ($attribute['system'] ?? false)) {
if ($attribute['type'] == TranslatedText::class) {
// Get the default language and make that the only one required.
$field = "{$attribute['signature']}.{$this->defaultLanguage->code}";
$isRequired = ($attribute['required'] ?? false) || ($attribute['system'] ?? false);

// TranslatedText values are in an array, apply rules to each item of the array
if ($attribute['type'] == TranslatedText::class) {
foreach ($this->languages as $language) {
// all rules set when attribute was created (resets on each iteration)
$validationRules = $validation;
if ($language->default && $isRequired) {
// append required for the default language
$validationRules = array_merge($validationRules, ['required']);
}
$rules["{$attribute['signature']}.{$language->code}"] = $validationRules;
}
continue;
}


if ($isRequired) {
$validation = array_merge($validation, ['required']);
}

if ($attribute['type'] == Number::class) {
$validation = array_merge($validation, [
'numeric'.($attribute['configuration']['min'] ? '|min:'.$attribute['configuration']['min'] : ''),
'numeric'.($attribute['configuration']['max'] ? '|max:'.$attribute['configuration']['max'] : ''),
'numeric' . ($attribute['configuration']['min'] ? '|min:' . $attribute['configuration']['min'] : ''),
'numeric' . ($attribute['configuration']['max'] ? '|max:' . $attribute['configuration']['max'] : ''),
]);
}

$rules[$field] = implode('|', $validation);
}

return $rules;
}

Expand Down Expand Up @@ -238,7 +250,7 @@ protected function withAttributesValidationMessages()
/**
* Handle attributes updated event.
*
* @param array $event
* @param array $event
* @return void
*/
public function updatedAttributes($event)
Expand Down
Loading

0 comments on commit 1a362cc

Please sign in to comment.