The selected {field} is invalid?

I keep getting this error saying that the selected fields are invalid whenever I try to submit this form. The form is located within a custom page. This is part of my form, but the rest of it is similar/the same:
public Defaults $defaultSetting;

public $account_id = '';
public $currency_code = '';
public $sales_tax_id = '';
public $purchase_tax_id = '';
public $income_category_id = '';
public $expense_category_id = '';

public function mount():void
{
$this->form->fill();
}

protected function getFormSchema(): array
{
return [
Section::make('General')
->schema([
Select::make('account_id')
->label('Account')
->relationship('account', 'name', static fn ($query) => $query->where('company_id', Auth::user()->currentCompany->id))
->searchable()
->default(Defaults::getDefaultAccount())
->required(),
Select::make('currency_code')
->label('Currency')
->relationship('currency', 'code', static fn ($query) => $query->where('company_id', Auth::user()->currentCompany->id))
->searchable()
->default(Defaults::getDefaultCurrency())
->required(),
])->columns(),
];
}

protected function getFormModel(): string
{
return Defaults::class;
}

public function create(): void
{
$defaultSetting = Defaults::create($this->form->getState());

$this->form->model($defaultSetting)->saveRelationships();
}

public function render(): View
{
return view('livewire.default-setting');
}
}
public Defaults $defaultSetting;

public $account_id = '';
public $currency_code = '';
public $sales_tax_id = '';
public $purchase_tax_id = '';
public $income_category_id = '';
public $expense_category_id = '';

public function mount():void
{
$this->form->fill();
}

protected function getFormSchema(): array
{
return [
Section::make('General')
->schema([
Select::make('account_id')
->label('Account')
->relationship('account', 'name', static fn ($query) => $query->where('company_id', Auth::user()->currentCompany->id))
->searchable()
->default(Defaults::getDefaultAccount())
->required(),
Select::make('currency_code')
->label('Currency')
->relationship('currency', 'code', static fn ($query) => $query->where('company_id', Auth::user()->currentCompany->id))
->searchable()
->default(Defaults::getDefaultCurrency())
->required(),
])->columns(),
];
}

protected function getFormModel(): string
{
return Defaults::class;
}

public function create(): void
{
$defaultSetting = Defaults::create($this->form->getState());

$this->form->model($defaultSetting)->saveRelationships();
}

public function render(): View
{
return view('livewire.default-setting');
}
}
22 Replies
Vp
Vp13mo ago
Did you try like this and remove ->default(De..) from select?
$this->form->fill([
'account_id' => Defaults::getDefaultAccount(),
'currency_code' => Defaults::getDefaultCurrency(),
]);
$this->form->fill([
'account_id' => Defaults::getDefaultAccount(),
'currency_code' => Defaults::getDefaultCurrency(),
]);
Andrew Wallo
Andrew Wallo13mo ago
Yes I did but I found out that the "relationship()" method was causing it. The regular options() method worked but idk why not relationship. And this happens when I use the searchable() and default() with the options now.
Section::make('General')
->schema([
Select::make('account_id')
->label('Account')
->options(Defaults::getAccounts())
->searchable()
->default(Defaults::getDefaultAccount())
->required(),
Select::make('currency_code')
->label('Currency')
->options(Defaults::getCurrencies())
->searchable()
->default(Defaults::getDefaultCurrency())
->required(),
])->columns(),
Section::make('General')
->schema([
Select::make('account_id')
->label('Account')
->options(Defaults::getAccounts())
->searchable()
->default(Defaults::getDefaultAccount())
->required(),
Select::make('currency_code')
->label('Currency')
->options(Defaults::getCurrencies())
->searchable()
->default(Defaults::getDefaultCurrency())
->required(),
])->columns(),
Andrew Wallo
Andrew Wallo13mo ago
Never have these issues when using Resources... And when clicking submit it says wrong integer value for them because there are multiple options with the same integer
toeknee
toeknee13mo ago
Try removing the defaults does that solve it? If so, try adding the defaults as part of the form fill You could also remove the defaults form the options
Andrew Wallo
Andrew Wallo13mo ago
Only until I removed the searchable() and also use disablePlaceholderSelection() then the right options are available, but then it makes the default the wrong default... Bank of America is supposed to be the default like before
Andrew Wallo
Andrew Wallo13mo ago
This is kinda frustrating ngl
Vp
Vp13mo ago
$this->form->fill([
'account_id' => auth()->user()->account_id, //this two lines are up-to-me, but you need to put Default Model ID here IMO
'currency_code' => auth()->user()->currency_code,
]);

Section::make('General')
->schema([
Select::make('account_id')
->label('Account')
->options(Defaults::getAccounts())
->searchable()
->required(),
Select::make('currency_code')
->label('Currency')
->options(Defaults::getCurrencies())
->searchable()
->required(),
])->columns(),
$this->form->fill([
'account_id' => auth()->user()->account_id, //this two lines are up-to-me, but you need to put Default Model ID here IMO
'currency_code' => auth()->user()->currency_code,
]);

Section::make('General')
->schema([
Select::make('account_id')
->label('Account')
->options(Defaults::getAccounts())
->searchable()
->required(),
Select::make('currency_code')
->label('Currency')
->options(Defaults::getCurrencies())
->searchable()
->required(),
])->columns(),
What is the result if you use like this?
toeknee
toeknee13mo ago
Hmm, default doesn't work by default when loading with data. are the accounts sorted in order? It is frustrating, I have seen it in the #althinect-spatie-roles-permissions when editing and adding permissions too.
Andrew Wallo
Andrew Wallo13mo ago
It just gives me multiple values that are the same like before. Even when I chose the correct one when I got to the point of removing searchable, etc, when clicking submit it would say “wrong integer value for account_id”, etc… which makes so since. I don’t think so but that shouldn’t matter to be honest, but do you think choices js is confusing option position values with the ID’s of the values in the database and multiplying them? Because I’ve noticed that when using “options()” with integers (Id) for keys and not strings the select field has issues a lot of the time
Vp
Vp13mo ago
Weird, I have 4 Input and 3 Select in my custom page, but I didn't have any error though.. for last, can you show your local scopes getAccounts() because I'm also curious 😆
Andrew Wallo
Andrew Wallo13mo ago
Andrew Wallo
Andrew Wallo13mo ago
These are defaults:
Andrew Wallo
Andrew Wallo13mo ago
This is what shows when I dd(getAccounts())
Andrew Wallo
Andrew Wallo13mo ago
Andrew Wallo
Andrew Wallo13mo ago
And this is literally what shows in the form when using that.
Vp
Vp13mo ago
Here you have 3 data, but in select UI you have 4 options.. this may be the cause of the error.. not sure tho
Andrew Wallo
Andrew Wallo13mo ago
I think its because when plucking an ID as the key, and when trying to use the default value you have to use that key as the default but the default has to be a string not a integer value or else it will use the key position and not its actually value
Andrew Wallo
Andrew Wallo13mo ago
Yeah nevermind because this worked so I suppose it was this
Vp
Vp13mo ago
Then change here ->get(['name', 'id'])->first()..... return $defaultAccount ?? null That's great if it's working..
Andrew Wallo
Andrew Wallo13mo ago
Haha so I was using defaultAccount->name ?? null before but its actually ID omfg
Andrew Wallo
Andrew Wallo13mo ago
I need some sleep lmao Thank you for all the help tho haha, its always the simple things
Vp
Vp13mo ago
WC, glad you found out..