Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix check Register AddBirthdayValidation.php #17

Closed
Litalino opened this issue Nov 24, 2023 · 1 comment
Closed

Fix check Register AddBirthdayValidation.php #17

Litalino opened this issue Nov 24, 2023 · 1 comment

Comments

@Litalino
Copy link
Contributor

Litalino commented Nov 24, 2023

public function __invoke(UserValidator $flarumValidator, Validator $validator): void
    {
        $rules = $validator->getRules();

        $isRequired = $this->settings->get('datlechin-birthdays.required')
            && $this->settings->get('datlechin-birthdays.set_on_registration');

        if ($isRequired  && ! $flarumValidator->getUser()) {
            $set_isRequired = 'required';
        } else {
            $set_isRequired = 'nullable';
        }

        $rules['birthday'] = [
            //'nullable',
            //Rule::requiredIf($isRequired && ! $flarumValidator->getUser()),
            $set_isRequired,
            'date_format:Y-m-d',
            'before:today',
            function ($attribute, $value, $fail) {
                if ($value) {
                    $birthday = new DateTime($value);
                    $now = new DateTime();
                    $diff = $now->diff($birthday);
                    if ($diff->y <= (int) $this->settings->get('datlechin-birthdays.min_age')) {
                        $fail($this->translator->trans('datlechin-birthdays.api.invalid_age_message', [
                            'minAge' => (int) $this->settings->get('datlechin-birthdays.min_age')
                        ]));
                    } else if ($diff->y > 100) {
                        $fail($this->translator->trans('datlechin-birthdays.api.invalid_dob_message'));
                    }
                }
            },
        ];

        $validator->setRules($rules);
    }

@datlechin
Copy link
Owner

Fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants