Skip to content

Commit

Permalink
Merge pull request #38 from optimistdigital/1.6.10
Browse files Browse the repository at this point in the history
1.6.10 release
  • Loading branch information
Tarpsvo authored Nov 19, 2020
2 parents d721959 + 47756a2 commit 56ba1ee
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 14 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.6.10] - 2020-11-19

### Added

- Added support for Slug field
- Added support for `->default` values

### Changed

- Changed `rulesFor` mixin to allow passing `array|callable|string` variables
- Fixed `formatRules` to include `->rules()` in locale specific rules.

## [1.6.9] - 2020-11-02

### Changed
Expand Down
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ Number::make('Population')

It's possible to define locale specific validation rules.

To do so, add the `->rulesFor()` on your field and the `HandlesTranslatable` trait to your Nova resource:
To do so, add the `->rulesFor()` on your field and the `HandlesTranslatable` trait to your Nova resource.

`->rulesFor` accepts `array|string|callable` locales and `array|callable` rules.

```php
use OptimistDigital\NovaTranslatable\HandlesTranslatable;
Expand All @@ -101,13 +103,20 @@ class Product extends Resource
->rules(['max:255'])
->rulesFor('en', [
'required',
'unique:products,sku->en,{{resourceId}}'
])
->rulesFor(['en', 'et'], function ($locale) {
return ["unique:products,name->$locale{{resourceId}}"];
}),
];
}
}
```

In this example, the rule `max` will be applied for `name.*` and the rule `required` will be applied for `name.fr`.
#### In this example, rules will be added to the following values
```dotenv
max: name.*
required: name.en
unique: name.en & name.et
```

## Configuration

Expand Down
2 changes: 1 addition & 1 deletion dist/js/translatable-field.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/HandlesTranslatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ protected static function formatRules(NovaRequest $request, array $rules)
foreach ($attributeRules['translatable'] as $locale => $localeRules) {
// We copy the locale rule into the rules array
// i.e. ['name.fr' => ['required']]
$rules[str_replace('.*', '', $attribute) . ".{$locale}"] = $localeRules;
$rules[str_replace('.*', '', $attribute) . ".{$locale}"] =
array_merge(collect($attributeRules)->except('translatable')->toArray(), $localeRules);

// We unset the translatable locale entry since we copy the rule into the rules array
unset($rules[$attribute]['translatable'][$locale]);
Expand Down
40 changes: 32 additions & 8 deletions src/TranslatableFieldMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Http\Requests\NovaRequest;

class TranslatableFieldMixin
{
Expand Down Expand Up @@ -45,16 +46,19 @@ public function translatable()
} catch (Exception $e) {
}

$value = array_map(function ($val) {
return !is_numeric($val) ? $val : (float) $val;
}, (array) $value);
if (!empty($value)) {
$value = array_map(function ($val) {
return !is_numeric($val) ? $val : (float) $val;
}, (array) $value);
}

$request = app(NovaRequest::class);
$this->withMeta([
'translatable' => [
'original_attribute' => $this->attribute,
'original_component' => $component,
'locales' => $locales,
'value' => $value
'value' => $value ?: ($this->resolveDefaultValue($request) ?? ""),
],
]);

Expand Down Expand Up @@ -109,12 +113,32 @@ public function translatable()

public function rulesFor()
{
return function ($locale, $rules) {
if (!in_array($locale, array_keys(FieldServiceProvider::getLocales()))) {
throw new Exception("Invalid locale specified ({$locale})");
return function ($locales, $rules) {
$setRule = function ($locale, $rules) {
if (!in_array($locale, array_keys(FieldServiceProvider::getLocales()))) {
throw new Exception("Invalid locale specified ({$locale})");
}

$this->rules['translatable'][$locale] = $rules;
return $this;
};

if (is_callable($locales)) $locales = call_user_func($locales);

// Array of locales or callable rules
if (is_array($locales) || is_callable($rules)) {
// Single locale with callable rules
if (!is_array($locales)) return $setRule($locales, call_user_func($rules, $locales));
foreach ($locales as $locale) {
$_rules = $rules;
if (is_callable($_rules)) $_rules = call_user_func($rules, $locale);
$setRule($locale, $_rules);
}

return $this;
}

$this->rules['translatable'][$locale] = $rules;
$setRule($locales, $rules);
return $this;
};
}
Expand Down

0 comments on commit 56ba1ee

Please sign in to comment.