|string> */ public function rules() : array { $rules = [ 'branch_code' => ['required', 'string', 'exists:branches,code'], 'account_number' => ['required', 'string'], 'is_period_range' => ['sometimes', 'boolean'], 'period_from' => ['required', 'string', 'regex:/^\d{6}$/'], // YYYYMM format ]; // If it's a period range, require period_to if ($this->input('is_period_range')) { $rules['period_to'] = [ 'required', 'string', 'regex:/^\d{6}$/', // YYYYMM format 'gte:period_from' // period_to must be greater than or equal to period_from ]; } return $rules; } /** * Get custom messages for validator errors. * * @return array */ public function messages() : array { return [ 'branch_code.required' => 'Branch code is required', 'branch_code.exists' => 'Selected branch does not exist', 'account_number.required' => 'Account number is required', 'period_from.required' => 'Period is required', 'period_from.regex' => 'Period must be in YYYYMM format', 'period_to.required' => 'End period is required for period range', 'period_to.regex' => 'End period must be in YYYYMM format', 'period_to.gte' => 'End period must be after or equal to start period', ]; } /** * Prepare the data for validation. * * @return void */ protected function prepareForValidation() : void { // Convert is_period_range to boolean if it exists if ($this->has('is_period_range')) { $this->merge([ 'is_period_range' => filter_var($this->is_period_range, FILTER_VALIDATE_BOOLEAN), ]); } } }