F
Filament13mo ago
Stevee

Validating Percentage Inputs in a Repeater

Hello everyone, I'm working on a project that involves a repeater for users to input percentages. I need to validate the input to meet the following criteria: The total combined percentage of all entries in the repeater should not exceed 100% and not < than 50. I'd appreciate any guidance or code examples on how to implement this validation effectively
3 Replies
Dennis Koch
Dennis Koch13mo ago
I think I'd write a custom validation rule (see Laravel docs) that runs over all entries and checks the sum
Stevee
SteveeOP13mo ago
oh where I can see it ?, only can find validation for specific field at the docs actually in v2, I do something like
$items = $this->getItems();
$total = 0;

foreach ($items as $item) {
if (isset($item['propertyA']) && $item['propertyA'] !== '0' && isset($item['propertyB'])) {
$total += (int) $item['propertyB'];
} else {
$total += 0;
}
}

if ($total > 100) {
throw ValidationException::withMessages(['items' => __('Total percentage cannot exceed 100%')]);
}

if ($total < 50) {
throw ValidationException::withMessages(['items' => __('Total percentage must be 50% or more')]);
}
$items = $this->getItems();
$total = 0;

foreach ($items as $item) {
if (isset($item['propertyA']) && $item['propertyA'] !== '0' && isset($item['propertyB'])) {
$total += (int) $item['propertyB'];
} else {
$total += 0;
}
}

if ($total > 100) {
throw ValidationException::withMessages(['items' => __('Total percentage cannot exceed 100%')]);
}

if ($total < 50) {
throw ValidationException::withMessages(['items' => __('Total percentage must be 50% or more')]);
}
but in v3, the ValidationException messages is not show and this also block others ValidationException to be shown bump
Dennis Koch
Dennis Koch13mo ago
You can find custom validation rules in the Laravel docs as a mentioned above.
Want results from more Discord servers?
Add your server