Closed miagg closed 2 years ago
that's also my problem.
@miagg @aliwesome I got around this issue as the following by extending the Translatable
class.
<?php
namespace App\Nova\Fields;
use Illuminate\Contracts\Validation\Rule;
use Laravel\Nova\Fields\Field;
use Spatie\NovaTranslatable\Translatable as SpatieTranslatable;
class Translatable extends SpatieTranslatable
{
/**
* Set custom validation rules for the fields.
*
* @param callable|array|string $rules
* @return $this
*/
public function rulesFor($rules = []): Translatable
{
$rules = ($rules instanceof Rule || is_string($rules)) ? func_get_args() : $rules;
collect($this->data)->each(function (Field $field) use ($rules) {
list(, $attr, $locale) = explode('_', $field->attribute);
$field->rules($rules[$attr][$locale]);
});
return $this;
}
protected function createTranslatedField(Field $originalField, string $locale): Field
{
$originalAttribute = $originalField->attribute;
$translatedField = parent::createTranslatedField($originalField, $locale);
$translatedField->attribute = 'translations_'.$originalAttribute.'_'.$locale;
return $translatedField;
}
}
And after that do something like the following.
use App\Nova\Fields\Translatable;
Translatable::make([
Trix::make('Biography')->withFiles('public'),
])->rulesFor([
'biography' => [
'en' => ['min:10'],
'om' => ['min:20'],
'am' => ['min:30'],
]
]),
Check support-different-rules branch of this sample project if you want to see it in action.
Note: If PR 68 is would be merged then no need to override the createTranslatedField
method.
Thanks :)
Dear contributor,
because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.
We need an option to use different rules for each locale. For example we might require one language (primary) but not all translations (nullable).