Workflow
Validation

This example illustrates the use of Validation Rules and Data Update.

You can switch the availablity of each Ice cream flavor.

Validation Rules

  • • The price must be in "#,## €" format, and between 2,00 € and 5,00 €.

  • • Each flavor can only appear once.

  • • Valid flavors are: chocolate, vanilla, strawberry, coconut, mint, and caramel.

ID
🍦 Flavor
Price
In Stock
1
2
3
Disclaimer: Table data is randomly generated for illustrative purposes only. The information here is not a reflection of the actual market and does not constitute business, financial, or medical advice.
<?php
 
namespace App\Livewire\Examples\ValidationTable;
 
use App\Models\Icecream;
use App\Rules\EuroCurrencyBetween2and5;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Number;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Footer;
use PowerComponents\LivewirePowerGrid\Header;
use PowerComponents\LivewirePowerGrid\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
 
final class ValidationTable extends PowerGridComponent
{
public array $flavor;
 
public array $price_in_eur;
 
public array $in_stock;
 
public bool $showErrorBag = true;
 
public function setUp(): array
{
return [
Header::make()
->showSearchInput(),
 
Footer::make()
->showPerPage()
->showRecordCount(),
];
}
 
public function datasource(): ?Builder
{
return Icecream::query();
}
 
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('flavor')
->add('in_stock')
->add('price_in_eur', fn ($icecream) => Number::currency($icecream->price, in: 'EUR', locale: 'pt_PT'));
}
 
public function columns(): array
{
return [
Column::make('ID', 'id'),
 
Column::make('🍦 Flavor', 'flavor')
->sortable()
->editOnClick(hasPermission: true)
->searchable(),
 
Column::make('Price', 'price_in_eur')
->editOnClick(hasPermission: true),
 
Column::make('In Stock', 'in_stock')
->toggleable(),
];
}
 
protected function rules()
{
return [
'flavor.*' => [
'required',
'in:chocolate,vanilla,strawberry,coconut,mint,caramel',
'unique:icecreams,flavor',
],
 
'in_stock.*' => [
'required',
'boolean',
],
 
'price_in_eur.*' => [
new EuroCurrencyBetween2and5(),
],
];
}
 
protected function validationAttributes()
{
return [
'flavor.*' => 'Ice cream flavor',
'price_in_eur.*' => 'Ice cream price',
];
}
 
protected function messages()
{
return [
'flavor.*.in' => 'Valid flavors: :values',
'flavor.*.unique' => 'Flavor already listed.',
];
}
 
public function onUpdatedEditable(string|int $id, string $field, string $value): void
{
$this->validate();
 
if ($field === 'price_in_eur') {
$field = 'price';
 
$value = (new \NumberFormatter('pt-PT', \NumberFormatter::CURRENCY))
->parse(preg_replace('/\s+/', "\u{A0}", $value));
}
 
Icecream::query()->find($id)->update([
$field => e($value),
]);
}
 
public function onUpdatedToggleable(string|int $id, string $field, string $value): void
{
Icecream::query()->find($id)->update([
$field => e($value),
]);
 
$this->skipRender();
}
}
Code highlighting provided by Torchlight.dev

Here you can find all relevant packages installed on this demo.

Name
Version
Description
laravel/framework
v11
The Laravel Framework.
livewire/livewire
v3.5.4
A front-end framework for Laravel.
openspout/openspout
v4.24.3
PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
power-components/livewire-powergrid
5.x-dev
PowerGrid generates Advanced Datatables using Laravel Livewire.