Reputation: 21
I am going through a tutorial and have watched the same video a few times to see if I had incorrect syntax on my form, but to me it looked ok. But when I hit 'edit' and then click save changes I get an error that the value already exists. This is happening on each of the forms I created.
I'm able to create a new form and I can edit a product if I change the product name and sku, which defeats the purpose, but it does record the changes in the database so I know the update functionality is working.
Here is the form code for the product:
public static function form(Form $form): Form
{
return $form
->schema([
Group::make()
->schema([
Section::make()
->schema([
TextInput::make('name')
->required()
->live(onBlur:true)
->unique()
->afterStateUpdated(function(string $operation, $state, Forms\Set $set){
if($operation!=='create'){
return;
}
$set('slug', Str::slug($state));
}),
TextInput::make('slug')
->disabled()
->dehydrated()
->required()
->unique(Product::class,'slug',ignoreRecord:true),
MarkdownEditor::make('description')
->columnSpan('full')
])->columns(2),
Section::make('Pricing & Inventory')
->schema([
TextInput::make('sku')
->label('SKU (Stock Keeping Unit)')
->required()
->unique(),
TextInput::make('price')
->numeric()
->rules(['regex:/^\d{1,6}(\.\d{0,2})?$/'])
->required(),
TextInput::make('quantity')
->numeric()
->minValue(0)
->maxValue(100)
->required(),
Select::make('type')
->options([
'downloadable' => ProductTypeEnum::DOWNLOADABLE->value,
'deliverable' => ProductTypeEnum::DELIVERABLE->value
])->required()
])->columns(2)
]),
Group::make()
->schema([
Section::make('Status')
->schema([
Toggle::make('is_visible')
->label("Visibility")
->helperText('Enable or disable product visibility')
->default(true),
Toggle::make('is_featured')
->label("Featured")
->helperText('Enable or disable product featured status'),
DatePicker::make('published_at')
->label('Avalability')
->default(now())
]),
Section::make('Image')
->schema([
FileUpload::make('image')
->directory('form-attachements')
->preserveFilenames()
->image()
->imageEditor()
])->collapsible(),
Section::make('Associations')
->schema([
Select::make('brand_id')
->relationship('brand','name')
]),
])
]);
}
Upvotes: 0
Views: 2729
Reputation: 21
I did find that if you use
->unique(ignoreRecord: true),
It seems to work. Odd in the tutorial, it doesn't show that, and it seems to work for the presenter.
Upvotes: 2