F
Filament15mo ago
Fonz

Custom validation rules

I am currently using a custom validation rule on a form. I would like to use the same rule in filament, but I do not know how. This is the rule: $request->validate([ "onlineRegistration" => [new OneOf($request, ["onlineRegistration","registrationLink","registrationEmail"])], "registrationLink" => [new OneOf($request, ["onlineRegistration","registrationLink","registrationEmail"])], "registrationEmail" => [new OneOf($request, ["onlineRegistration","registrationLink","registrationEmail"])], ]); In filament, I tried this: Toggle::make('onlineRegistration')->rules([new OneOf(request(), ["onlineRegistration","registrationLink","registrationEmail"])]), But it always shows a validation error
6 Replies
cheesegrits
cheesegrits15mo ago
What is OneOf? Afaik that's not standard Laravel. I assume it's some custom class that checks to see if one of the provided array values exists in the given object. But Filament doesn't use request(), it's Livewire. Describe what your form and validation is actually trying to achieve.
Fonz
FonzOP15mo ago
Stack Overflow
Laravel validation rule for either of two fields required but both ...
Is there a Laravel validation rule for when either of two fields are required but both should not be present at the same time. Eg. Mobile number and email, either of them should be present but not ...
Fonz
FonzOP15mo ago
I have three fields and only one of those should be present
cheesegrits
cheesegrits15mo ago
Yeah, that's not going to work for Filament, as it doesn't use the $request object. Livewire is not controller / request based. You'll need to roll your own custom validation using $get ... https://filamentphp.com/docs/3.x/forms/validation#custom-rules
cheesegrits
cheesegrits15mo ago
Maybe something like ...
Toggle::make('onlineRegistration')
->rules([
fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
$filledCount = 0;
foreach(["onlineRegistration","registrationLink","registrationEmail"] as $field) {
if ($get($field) {
$filledCount++;
}
}

if ($filled !== 1) {
$fail('Only one of blah blah may be selected');
}
},
]),
Toggle::make('onlineRegistration')
->rules([
fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
$filledCount = 0;
foreach(["onlineRegistration","registrationLink","registrationEmail"] as $field) {
if ($get($field) {
$filledCount++;
}
}

if ($filled !== 1) {
$fail('Only one of blah blah may be selected');
}
},
]),
Fonz
FonzOP15mo ago
Thanks, this works, just had change it a bit so it works with v2:
->rules([
function (Closure $get) {
return function (string $attribute, $value, Closure $fail) use ($get) {
$filledCount = 0;
foreach(["registrationLink","registrationEmail"] as $field) {
if ($get($field)) {
$filledCount++;
}
}
if ($get("onlineRegistration") == 1) {
$filledCount++;
}
if ($filledCount !== 1) {
$fail('Only one of may be selected');
}
};
}
]),
->rules([
function (Closure $get) {
return function (string $attribute, $value, Closure $fail) use ($get) {
$filledCount = 0;
foreach(["registrationLink","registrationEmail"] as $field) {
if ($get($field)) {
$filledCount++;
}
}
if ($get("onlineRegistration") == 1) {
$filledCount++;
}
if ($filledCount !== 1) {
$fail('Only one of may be selected');
}
};
}
]),
Want results from more Discord servers?
Add your server