make repeater validation

I have Repeater where i can add many blogs. this blogs has amount. this blogs amount sum should not be more then sum amount . I want to make validation if first brand amount is 500 and second brand amount i wrote 300 and sum amount === 600 second brand amount should have erorr that max value is 100. there is my code:
php
Repeater::make('purchaseBrands')
->relationship()
->schema([
Select::make('brand_id')
->label('Brand')
->relationship('brand', 'name', function (Builder $query, Get $get) {
if($get('../../supplier_id') === null) {
return $query;
}
$brandIds = Supplier::find($get('../../supplier_id'))->brand_id;
$query->whereIn('id', $brandIds);
})
->searchable()
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->required()
->preload()
->createOptionForm([
TextInput::make('name')
->required()
->label('New Brand'),
TextInput::make('brand_amount')
->numeric()
->required()
]),
TextInput::make('amount')->required()
->required()
->numeric()
->live()
->maxValue()

])
->columns(2)
->addActionLabel('add blog')
->collapsible(),
php
Repeater::make('purchaseBrands')
->relationship()
->schema([
Select::make('brand_id')
->label('Brand')
->relationship('brand', 'name', function (Builder $query, Get $get) {
if($get('../../supplier_id') === null) {
return $query;
}
$brandIds = Supplier::find($get('../../supplier_id'))->brand_id;
$query->whereIn('id', $brandIds);
})
->searchable()
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->required()
->preload()
->createOptionForm([
TextInput::make('name')
->required()
->label('New Brand'),
TextInput::make('brand_amount')
->numeric()
->required()
]),
TextInput::make('amount')->required()
->required()
->numeric()
->live()
->maxValue()

])
->columns(2)
->addActionLabel('add blog')
->collapsible(),
29 Replies
gigiloouu
gigiloouu5mo ago
No description
gigiloouu
gigiloouu5mo ago
anyone can help?..
toeknee
toeknee5mo ago
Your code blocks should start with 3 x ` and then php so it formats it in php code block. You need to explain more what you want. I am not following your issue.
Dennis Koch
Dennis Koch5mo ago
@gigiloouu For the next time: Please don't open mutilple threads for the same issue. I think you can use the mutateDataBeforeSave hook on the form to manually validate the data and throw a Validation Exception. That's probably the only way
gigiloouu
gigiloouu5mo ago
sorry, i just wanted to write my problem more detail okay thanks i will try.. u mean use mutateFormDataBeforeCreate becouse i cant find any function or article about mutateDataBeforeSave
Dennis Koch
Dennis Koch5mo ago
Yes
gigiloouu
gigiloouu5mo ago
i try and google but i cant do it can u help me with that?
gigiloouu
gigiloouu5mo ago
i read this already and when i wrote this function its not working. like i try to just dd(); data but not working i mean im getting data but i dont have brands data
gigiloouu
gigiloouu5mo ago
No description
gigiloouu
gigiloouu5mo ago
im using pivot table for brands
toeknee
toeknee5mo ago
ahh it won't work, ennis is slightly wrong as it's relationship data it's kept out of it, I think you need a different method.
gigiloouu
gigiloouu5mo ago
hm for another task i just use maxValue and next i wrote some logic to see max value what will be but at this moment I have no idea how many brands will be becouse u can add as many as u wish can u give me some idea or hint what will be good for this issue 😢
toeknee
toeknee5mo ago
So what is the overall goal you are trying to achieve?
gigiloouu
gigiloouu5mo ago
okay i will tell u
toeknee
toeknee5mo ago
Please try to write as clear as possible.
gigiloouu
gigiloouu5mo ago
I have purchase table where i have Sum Amount
gigiloouu
gigiloouu5mo ago
No description
gigiloouu
gigiloouu5mo ago
also purchase have brands, becouse of brands is many to many i use pivot table
gigiloouu
gigiloouu5mo ago
No description
gigiloouu
gigiloouu5mo ago
u can click add blog and its add new feal for new brand all brands has their own amount so all this brands amount should be equal of this sum amount like if brands amount is more then sum amount we should have a error if its less should be error it should be equal
toeknee
toeknee5mo ago
Where is sum amount entered? Earlier in the form?
gigiloouu
gigiloouu5mo ago
brands amount sum should be equal of sum amount
gigiloouu
gigiloouu5mo ago
No description
gigiloouu
gigiloouu5mo ago
form looks like this i can make sum amount earlier its not a problem
toeknee
toeknee5mo ago
So use a custom validation rule?
->rules([
fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {

$max = 0;
$current = 0;
$brands = $get('../purchaseBrands', []);

foreach($brands as $brand) {
$current += $brand['amount'];
}

if( $max !== $current ) {
$fail("The sum is ' . $max . ' and the current is ' . $current' . ' Please ensure the sum is the same as all brands counted.");
}
},
])
->rules([
fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {

$max = 0;
$current = 0;
$brands = $get('../purchaseBrands', []);

foreach($brands as $brand) {
$current += $brand['amount'];
}

if( $max !== $current ) {
$fail("The sum is ' . $max . ' and the current is ' . $current' . ' Please ensure the sum is the same as all brands counted.");
}
},
])
Or similar to that.
gigiloouu
gigiloouu5mo ago
Filament\Forms\Get::__invoke(): Argument #2 ($isAbsolute) must be of type bool, array given, called in /var/www/html/shesyidvebi/app/Filament/Resources/PurchaseResource.php on line 126
Filament\Forms\Get::__invoke(): Argument #2 ($isAbsolute) must be of type bool, array given, called in /var/www/html/shesyidvebi/app/Filament/Resources/PurchaseResource.php on line 126
i have error in there
$brands = $get('../purchaseBrands', []);
$brands = $get('../purchaseBrands', []);
i should change empty are of false ?
toeknee
toeknee5mo ago
git it a try, it's just a rough free hand code up from memory. But gives you the runderstanding. see: https://filamentphp.com/docs/3.x/forms/validation#custom-rules for the docs
gigiloouu
gigiloouu5mo ago
thats works thanks u soo much ❤️ for ur time ❤️