user() !== null; } /** * Get the validation rules that apply to the request. * * @return array|string> */ public function rules(): array { return [ 'dateOfBirth' => [ 'required', 'date_format:Y-m-d', 'before_or_equal:'.now()->subYears(18)->toDateString(), 'after_or_equal:'.now()->subYears(100)->toDateString(), ], 'sex' => ['required', Rule::in([UserSex::MAN->value, UserSex::WOMAN->value])], 'heightCm' => ['required', 'integer', 'min:120', 'max:230'], 'weightKg' => ['required', 'numeric', 'min:35', 'max:300'], 'targetWeightKg' => ['nullable', 'numeric', 'min:35', 'max:300'], 'physicalActivityLevel' => ['required', Rule::enum(PhysicalActivityLevel::class)], 'weightGoal' => ['required', Rule::enum(WeightGoal::class)], 'pacePreference' => ['required', Rule::enum(PacePreference::class)], 'nutritionEstimateAccepted' => ['required', 'accepted'], ]; } public function after(): array { return [ function (Validator $validator): void { $weight = $this->float('weightKg'); $targetWeight = $this->input('targetWeightKg'); $goal = $this->string('weightGoal')->toString(); if ($targetWeight === null || $targetWeight === '') { return; } $targetWeight = (float) $targetWeight; if ($goal === WeightGoal::LOSE_WEIGHT->value && $targetWeight >= $weight) { $validator->errors()->add('targetWeightKg', 'NUTRITION_TARGET_WEIGHT_MUST_BE_LOWER'); } if ($goal === WeightGoal::GAIN_WEIGHT->value && $targetWeight <= $weight) { $validator->errors()->add('targetWeightKg', 'NUTRITION_TARGET_WEIGHT_MUST_BE_HIGHER'); } }, ]; } public function messages(): array { return [ 'dateOfBirth.before_or_equal' => 'NUTRITION_ADULTS_ONLY', 'dateOfBirth.after_or_equal' => 'NUTRITION_BIRTH_DATE_OUT_OF_RANGE', 'sex.in' => 'NUTRITION_METABOLIC_SEX_REQUIRED', 'nutritionEstimateAccepted.accepted' => 'NUTRITION_ESTIMATE_ACCEPTANCE_REQUIRED', ]; } }