F
Filament3mo ago
zemo

if I use createOptionUsing (along with createOptionForm), do I have to validate the inputs manually?

in my createOptionForm I cannot seem to use Filament validation rules for the inputs. Is it because I'm using createOptionUsing? do I have to call the Illuminate\Support\Facades\Validator and validate manually?
19 Replies
toeknee
toeknee3mo ago
It shoudn't matter as far as I am aware, what error are you getting?
zemo
zemoOP3mo ago
no error, it simply isn't working ->unique(modifyRuleUsing: function (Unique $rule, Get $get) { ray($get('subject_id')); return $rule->where('subject_id', $get('subject_id'))->withoutTrashed(); }) I'm also using this same rule in a normal form (no createOptionForm) and it is working.
toeknee
toeknee3mo ago
try adding ->fillForm([]) else, refer to what error is shown
zemo
zemoOP3mo ago
what is this used for and where do I add it?
toeknee
toeknee3mo ago
->fillForm([])
->unique(modifyRuleUsing: function (Unique $rule, Get $get) {
ray($get('subject_id'));
return $rule->where('subject_id', $get('subject_id'))->withoutTrashed();
})
->fillForm([])
->unique(modifyRuleUsing: function (Unique $rule, Get $get) {
ray($get('subject_id'));
return $rule->where('subject_id', $get('subject_id'))->withoutTrashed();
})
zemo
zemoOP3mo ago
error: Method Filament\Forms\Components\Select::fillForm does not exist. (same with TextInput)
toeknee
toeknee3mo ago
Sorry wrong chat. Unique should work, if you DD in the fucntion what happens? or error_log? is it hit?
zemo
zemoOP3mo ago
nothing, the dd is not fired at all neither the error_log() (I think, I've never used this but placing it in the callback fn before the return is doing nothing)
toeknee
toeknee3mo ago
Can you provide the whole code?
zemo
zemoOP3mo ago
yes, thanks one sec goddamnit the message is too long lol
zemo
zemoOP3mo ago
I hope this is fine as well
toeknee
toeknee3mo ago
can you run php artisan about and tell me the filament version
zemo
zemoOP3mo ago
v3.2.96
toeknee
toeknee3mo ago
Strange, try moving the creatoptionform to be first, but as far as I am aware it should work I'd also upgrade to the latest version to be safe
zemo
zemoOP3mo ago
did both, not working the very strange thing is that the dd() or the ray() are not firing.. cos if they fired I would've known that the issue had to do with the fn ahhh maybe I found a logic reason as to why it is not working Forms\Components\Select::make('subscription_id') doesn't have a relationship() so perhaps it does not know which table to check for the unique validation rule?
toeknee
toeknee3mo ago
no it would still fire, what if you do a custom validation rule
zemo
zemoOP3mo ago
like the one I was trying to do (which is commented)? in the createOptionUsing
$validator = Validator::make($data, [
Rule::unique('subscriptions')->where(function ($query) use($data) {
ray('prova');
return $query->where('subject_id', $data['subject_id'])
->where('subscription_year_id', $data['subscription_year_id']);
}),
]);
if ($validator->fails()) {
ray('ciao');
return null;
}
$validator = Validator::make($data, [
Rule::unique('subscriptions')->where(function ($query) use($data) {
ray('prova');
return $query->where('subject_id', $data['subject_id'])
->where('subscription_year_id', $data['subscription_year_id']);
}),
]);
if ($validator->fails()) {
ray('ciao');
return null;
}
even this was not working though, I just did this and it appears to do what I'm trying to do. might stick with it at this point
->createOptionUsing(function (array $data) {
$subscription = Subscription::where('subject_id', $data['subject_id'])->where('subscription_year_id', $data['subscription_year_id'])->first();

if ($subscription) {
return;
}

$subscription = new Subscription();
$subscription->subject_id = $data['subject_id'];
$subscription->subscription_year_id = $data['subscription_year_id'];
$subscription->federation_id = $data['federation_id'];
$subscription->date = $data['date'];
$subscription->price = $data['price'];
$subscription->save();
return $subscription->id;
})
->createOptionUsing(function (array $data) {
$subscription = Subscription::where('subject_id', $data['subject_id'])->where('subscription_year_id', $data['subscription_year_id'])->first();

if ($subscription) {
return;
}

$subscription = new Subscription();
$subscription->subject_id = $data['subject_id'];
$subscription->subscription_year_id = $data['subscription_year_id'];
$subscription->federation_id = $data['federation_id'];
$subscription->date = $data['date'];
$subscription->price = $data['price'];
$subscription->save();
return $subscription->id;
})
Want results from more Discord servers?
Add your server