Skip to content

Commit

Permalink
Fixing a unique validation problem with field edit
Browse files Browse the repository at this point in the history
  • Loading branch information
ModestasV committed May 10, 2024
1 parent b84df04 commit 5831a2e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Enums\CrudFieldValidation;
use App\Enums\CrudTypes;
use App\Models\Crud;
use App\Models\CrudField;
use App\Models\Panel;
use Filament\Facades\Filament;
use Filament\Forms;
Expand Down Expand Up @@ -41,7 +42,7 @@ public function form(Form $form): Form
->label('Relationship Options')
->relationship('crudFieldOptions')
->hidden(function (Forms\Get $get) {
return ! in_array($get('type'), [CrudFieldTypes::BELONGS_TO->value, CrudFieldTypes::BELONGS_TO_MANY->value]);
return !in_array($get('type'), [CrudFieldTypes::BELONGS_TO->value, CrudFieldTypes::BELONGS_TO_MANY->value]);
})
->schema([
Forms\Components\Select::make('crud_id')
Expand Down Expand Up @@ -74,18 +75,18 @@ public function form(Form $form): Form

$crud = $panel->cruds()->find($get('crud_id'));

if (! $crud) {
if (!$crud) {
return [];
}

return $crud->fields()->pluck('label', 'id')->toArray();
})
->hidden(function (Forms\Get $get) {
if (! in_array($get('../type'), [CrudFieldTypes::BELONGS_TO->value, CrudFieldTypes::BELONGS_TO_MANY->value])) {
if (!in_array($get('../type'), [CrudFieldTypes::BELONGS_TO->value, CrudFieldTypes::BELONGS_TO_MANY->value])) {
return true;
}

if (! $get('crud_id')) {
if (!$get('crud_id')) {
return true;
}

Expand All @@ -94,24 +95,33 @@ public function form(Form $form): Form
]),
Forms\Components\TextInput::make('label')
->required()
->unique(modifyRuleUsing: function (Forms\Get $get, Unique $rule) {
if ($get('type') === CrudFieldTypes::BELONGS_TO->value) {
->unique(modifyRuleUsing: function (Forms\Get $get, Unique $rule, ?CrudField $record) {

if ($record) {
$key = $record->key;
} else if ($get('type') === CrudFieldTypes::BELONGS_TO->value) {
$key = str($get('label')) // @phpstan-ignore-line
->lower()
->snake()
->toString().'_id';
->snake()
->toString() . '_id';
} else {
$key = str($get('label'))// @phpstan-ignore-line
->lower()
->lower()
->snake()
->toString();
}

/** @var Crud $crud */
$crud = $this->getOwnerRecord();

return $rule->where('crud_id', $crud->id)
$returnRule = $rule->where('crud_id', $crud->id)
->where('key', $key);

if ($record) {
$returnRule->ignore($record->id);
}

return $returnRule;
})
->maxLength(255),
Forms\Components\TextInput::make('tooltip')
Expand All @@ -129,12 +139,12 @@ public function form(Form $form): Form
$owner = $this->getOwnerRecord();

return $owner->fields()
->whereNotIn('key', [
'created_at',
'updated_at',
'deleted_at',
])
->max('order') + 1;
->whereNotIn('key', [
'created_at',
'updated_at',
'deleted_at',
])
->max('order') + 1;
}),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"framework",
"filament"
],
"version": "1.0.3",
"version": "1.0.4",
"license": "MIT",
"require": {
"php": "^8.2",
Expand Down

0 comments on commit 5831a2e

Please sign in to comment.