Skip to content

Commit

Permalink
added better support of asset id (000-000)
Browse files Browse the repository at this point in the history
  • Loading branch information
Flatroy committed Jun 21, 2024
1 parent 89fee0e commit d93cc26
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/Filament/Resources/ItemResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@ public static function form(Form $form): Form

TextInput::make('asset_id')
->label('Asset ID')
->integer(),
// must be like 000-000 but stored as int
->rules([
'required',
'regex:/^\d{3}-\d{3}$/',
])
->hint('Must be in format 000-000')
,

Checkbox::make('insured')->label('Insured'),

Expand Down
11 changes: 11 additions & 0 deletions app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,15 @@ public static function generateUlid(): string
{
return strtolower((string) Str::ulid());
}

public function getAssetIdAttribute($value)
{
return $value ? sprintf('%03d-%03d', $value / 1000, $value % 1000) : null;
}
public function setAssetIdAttribute($value)
{
$value = preg_replace('/[^0-9]/', '', $value);

$this->attributes['asset_id'] = (int) $value;
}
}

0 comments on commit d93cc26

Please sign in to comment.