Header Ads

Header ADS

Laravel Filament form validation with conditionally insert and update

 TextInput::make('domain')

                ->label('Store Domain')
                ->prefix('https://')
                ->rules(function ($get) {
                    $rules = [
                        'regex:/^[a-zA-Z0-9]+$/',
                        'nullable', // Allow nullable (optional) during update
                    ];

                    // Apply these rules only if it's an insert (creation)
                    if (!$get('id')) {
                        $rules[] = 'required'; // Make domain required during insertion
                        $rules[] = 'unique:stores,domain'; // Ensure the domain is unique during insertion
                    } else {
                        // Exclude current record from the unique validation during update
                        $rules[] = 'unique:stores,domain,' . $get('id');
                    }

                    return $rules;
                })
                ->suffix('.'.parse_url(env('APP_URL'), PHP_URL_HOST).'.com'),

No comments

Theme images by fpm. Powered by Blogger.