refactor(validation): implement centralized validation patterns across components
- Introduced `ValidationPatterns` class to standardize validation rules and messages for various fields across multiple components. - Updated components including `General`, `StackForm`, `Create`, and `Show` to utilize the new validation patterns, ensuring consistent validation logic. - Enhanced error messages for required fields and added regex validation for names and descriptions to improve user feedback. - Adjusted styling in the `create.blade.php` view for better visual hierarchy.
This commit is contained in:
@@ -6,82 +6,60 @@ use App\Actions\Server\StartSentinel;
|
||||
use App\Actions\Server\StopSentinel;
|
||||
use App\Events\ServerReachabilityChanged;
|
||||
use App\Models\Server;
|
||||
use App\Support\ValidationPatterns;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Attributes\Validate;
|
||||
use Livewire\Component;
|
||||
|
||||
class Show extends Component
|
||||
{
|
||||
public Server $server;
|
||||
|
||||
#[Validate(['required'])]
|
||||
public string $name;
|
||||
|
||||
#[Validate(['nullable'])]
|
||||
public ?string $description = null;
|
||||
|
||||
#[Validate(['required'])]
|
||||
public string $ip;
|
||||
|
||||
#[Validate(['required'])]
|
||||
public string $user;
|
||||
|
||||
#[Validate(['required'])]
|
||||
public string $port;
|
||||
|
||||
#[Validate(['nullable'])]
|
||||
public ?string $validationLogs = null;
|
||||
|
||||
#[Validate(['nullable', 'url'])]
|
||||
public ?string $wildcardDomain = null;
|
||||
|
||||
#[Validate(['required'])]
|
||||
public bool $isReachable;
|
||||
|
||||
#[Validate(['required'])]
|
||||
public bool $isUsable;
|
||||
|
||||
#[Validate(['required'])]
|
||||
public bool $isSwarmManager;
|
||||
|
||||
#[Validate(['required'])]
|
||||
public bool $isSwarmWorker;
|
||||
|
||||
#[Validate(['required'])]
|
||||
public bool $isBuildServer;
|
||||
|
||||
#[Locked]
|
||||
public bool $isBuildServerLocked = false;
|
||||
|
||||
#[Validate(['required'])]
|
||||
public bool $isMetricsEnabled;
|
||||
|
||||
#[Validate(['required'])]
|
||||
public string $sentinelToken;
|
||||
|
||||
#[Validate(['nullable'])]
|
||||
public ?string $sentinelUpdatedAt = null;
|
||||
|
||||
#[Validate(['required', 'integer', 'min:1'])]
|
||||
public int $sentinelMetricsRefreshRateSeconds;
|
||||
|
||||
#[Validate(['required', 'integer', 'min:1'])]
|
||||
public int $sentinelMetricsHistoryDays;
|
||||
|
||||
#[Validate(['required', 'integer', 'min:10'])]
|
||||
public int $sentinelPushIntervalSeconds;
|
||||
|
||||
#[Validate(['nullable', 'url'])]
|
||||
public ?string $sentinelCustomUrl = null;
|
||||
|
||||
#[Validate(['required'])]
|
||||
public bool $isSentinelEnabled;
|
||||
|
||||
#[Validate(['required'])]
|
||||
public bool $isSentinelDebugEnabled;
|
||||
|
||||
#[Validate(['required'])]
|
||||
public string $serverTimezone;
|
||||
|
||||
public function getListeners()
|
||||
@@ -91,6 +69,59 @@ class Show extends Component
|
||||
];
|
||||
}
|
||||
|
||||
protected function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ValidationPatterns::nameRules(),
|
||||
'description' => ValidationPatterns::descriptionRules(),
|
||||
'ip' => 'required',
|
||||
'user' => 'required',
|
||||
'port' => 'required',
|
||||
'validationLogs' => 'nullable',
|
||||
'wildcardDomain' => 'nullable|url',
|
||||
'isReachable' => 'required',
|
||||
'isUsable' => 'required',
|
||||
'isSwarmManager' => 'required',
|
||||
'isSwarmWorker' => 'required',
|
||||
'isBuildServer' => 'required',
|
||||
'isMetricsEnabled' => 'required',
|
||||
'sentinelToken' => 'required',
|
||||
'sentinelUpdatedAt' => 'nullable',
|
||||
'sentinelMetricsRefreshRateSeconds' => 'required|integer|min:1',
|
||||
'sentinelMetricsHistoryDays' => 'required|integer|min:1',
|
||||
'sentinelPushIntervalSeconds' => 'required|integer|min:10',
|
||||
'sentinelCustomUrl' => 'nullable|url',
|
||||
'isSentinelEnabled' => 'required',
|
||||
'isSentinelDebugEnabled' => 'required',
|
||||
'serverTimezone' => 'required',
|
||||
];
|
||||
}
|
||||
|
||||
protected function messages(): array
|
||||
{
|
||||
return array_merge(
|
||||
ValidationPatterns::combinedMessages(),
|
||||
[
|
||||
'ip.required' => 'The IP Address field is required.',
|
||||
'user.required' => 'The User field is required.',
|
||||
'port.required' => 'The Port field is required.',
|
||||
'wildcardDomain.url' => 'The Wildcard Domain must be a valid URL.',
|
||||
'sentinelToken.required' => 'The Sentinel Token field is required.',
|
||||
'sentinelMetricsRefreshRateSeconds.required' => 'The Metrics Refresh Rate field is required.',
|
||||
'sentinelMetricsRefreshRateSeconds.integer' => 'The Metrics Refresh Rate must be an integer.',
|
||||
'sentinelMetricsRefreshRateSeconds.min' => 'The Metrics Refresh Rate must be at least 1 second.',
|
||||
'sentinelMetricsHistoryDays.required' => 'The Metrics History Days field is required.',
|
||||
'sentinelMetricsHistoryDays.integer' => 'The Metrics History Days must be an integer.',
|
||||
'sentinelMetricsHistoryDays.min' => 'The Metrics History Days must be at least 1 day.',
|
||||
'sentinelPushIntervalSeconds.required' => 'The Push Interval field is required.',
|
||||
'sentinelPushIntervalSeconds.integer' => 'The Push Interval must be an integer.',
|
||||
'sentinelPushIntervalSeconds.min' => 'The Push Interval must be at least 10 seconds.',
|
||||
'sentinelCustomUrl.url' => 'The Custom Sentinel URL must be a valid URL.',
|
||||
'serverTimezone.required' => 'The Server Timezone field is required.',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function mount(string $server_uuid)
|
||||
{
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user