[ 'required', 'string', 'max:255', 'regex:/^\d{4}-\d{2}$/', // Format YYYY-MM ], 'notes' => [ 'nullable', 'string', ], ]; // If we're updating an existing record, add a unique constraint that ignores the current record if ($this->isMethod('PUT') || $this->isMethod('PATCH')) { $rules['periode'][] = Rule::unique('periode_statements', 'periode') ->ignore($this->route('periode_statement')); } else { // For new records, just check uniqueness $rules['periode'][] = 'unique:periode_statements,periode'; } return $rules; } /** * Get custom attributes for validator errors. */ public function attributes() : array { return [ 'periode' => 'Periode', 'notes' => 'Notes', ]; } /** * Get the error messages for the defined validation rules. */ public function messages() : array { return [ 'periode.required' => 'The periode field is required.', 'periode.unique' => 'This periode already exists.', 'periode.regex' => 'The periode must be in the format YYYY-MM (e.g., 2023-01).', ]; } }