replace($this->normalizedInput()); } public function validationData() { return $this->normalizedInput(); } protected function passedValidation() { $this->replace($this->normalizedInput()); } public function rules() { $company = session()->get('currentCompany'); $service = app(LeadFieldSettingsService::class); $rules = $service->getValidationRules(auth()->user()); if (array_key_exists('email', $rules) || $this->filled('email')) { $emailRules = $rules['email'] ?? ['nullable', 'email', 'max:255']; $emailRules[] = Rule::unique('leads')->where(function ($query) use ($company) { $query->where('company_id', '=', $company->id); $query->whereNull('deleted_at'); }); $emailRules[] = Rule::unique('lead_email')->where(function ($query) use ($company) { $query->where('company_id', '=', $company->id); $query->whereNull('deleted_at'); }); $emailRules[] = Rule::unique('contacts')->where(function ($query) use ($company) { $query->where('company_id', '=', $company->id); $query->whereNull('deleted_at'); }); $rules['email'] = $emailRules; } if (!isset($rules['first_name'])) { $rules['first_name'] = ['required', 'string', 'max:255']; } return $rules; } public function withValidator($validator) { $validator->after(function ($validator) { $service = app(LeadFieldSettingsService::class); $errors = $service->validateDependencies(auth()->user(), $this->all()); foreach ($errors as $fieldKey => $messages) { foreach ($messages as $message) { $validator->errors()->add($fieldKey, $message); } } $company = session()->get('currentCompany'); $phone = $this->phone; $mobile = $this->mobile; if (!empty($phone)) { $existingPhone = \DB::table('leads') ->where('company_id', $company->id) ->where('phone', $phone) ->whereNull('deleted_at') ->latest('id') ->first(); if ($existingPhone) { flash("Warning: The phone number already exists in the system (Lead ID: {$existingPhone->id}).")->warning(); } } if (!empty($mobile)) { $existingMobile = \DB::table('leads') ->where('company_id', $company->id) ->where('mobile', $mobile) ->whereNull('deleted_at') ->latest('id') ->first(); if ($existingMobile) { flash("Warning: The mobile number already exists in the system (Lead ID: {$existingMobile->id}).")->warning(); } } }); } private function normalizedInput(): array { if (is_array($this->normalizedLeadInput)) { return $this->normalizedLeadInput; } $input = $this->all(); $user = auth()->user(); if ($user) { $input = app(LeadFieldSettingsService::class)->normalizeMultiValueInput($user, $input); } // Always normalise phone/mobile so the value is saved to DB. // Visibility is controlled by LeadFieldSettingsService (show_cid); // the number must be stored regardless. $input['phone'] = $this->phone ? preg_replace('/\s+/', '', $this->phone) : null; $input['mobile'] = $this->mobile ? preg_replace('/\s+/', '', $this->mobile) : null; // Enforce leads.assign: users without it cannot set the assignment on creation. if ($user && ! $user->hasPermissionTo('leads.assign')) { unset($input['owned_by'], $input['owned_by_id'], $input['owned_by_type']); } return $this->normalizedLeadInput = $input; } }