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

feat: add closure validation rule #6951

Merged
merged 9 commits into from Dec 12, 2022

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Dec 7, 2022

Description

  • add closure validation rule
        $this->validation->setRules(
            [
                'foo' => ['required', static fn ($value) => $value === 'abc'],
            ],
            [
                // Errors
                'foo' => [
                    // Specify the array key for the closure rule.
                    1 => 'The value is not "abc"',
                ],
            ],
        );
        $this->validation->setRules([
            'foo' => [
                'required',
                static function ($value, $data, &$error, $field) {
                    if ($value !== 'abc') {
                        $error = 'The ' . $field . ' value is not "abc"';

                        return false;
                    }

                    return true;
                },
            ],
        ]);

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added enhancement PRs that improve existing functionalities 4.3 labels Dec 7, 2022
@michalsn michalsn added the docs needed Pull requests needing documentation write-ups and/or revisions. label Dec 7, 2022
@kenjis kenjis force-pushed the feat-closure-validation-rule branch from a133002 to b954991 Compare December 8, 2022 07:41
@kenjis kenjis removed the docs needed Pull requests needing documentation write-ups and/or revisions. label Dec 8, 2022
@kenjis
Copy link
Member Author

kenjis commented Dec 8, 2022

Added the docs.

Copy link
Contributor

@datamweb datamweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good feat👌

@@ -333,7 +334,7 @@ protected function processRules(

// @phpstan-ignore-next-line $error may be set by rule methods.
$this->errors[$field] = $error ?? $this->getErrorMessage(
$rule,
$this->isClosure($rule) ? $i : $rule,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't find this any easier to read than using instanceof inline.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not against it. However, this method would make more sense if we used it more than once.

tests/system/Validation/ValidationTest.php Show resolved Hide resolved
@kenjis
Copy link
Member Author

kenjis commented Dec 11, 2022

Added the closure parameters.

Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvements.

Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the new closure method definition, but glad it is optional.

Copy link
Contributor

@datamweb datamweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kenjis code works well.

form

I don't know why, but I think it should cover feature label for convenience. I used the following method to translate the field name. But I would have preferred it as a label.

            $validation->setRules(
                [
                    'foo' => [
                        'required',
                        static function ($value, $data, &$error, $field) {
                            if ($value !== 'abc') {
                                $translateField = lang("Foo.$field");
                                $error = 'The ' . $translateField . ' value is not "abc"';

                                return false;
                            }

                            return true;
                        },
                    ],
                ],
            );

@kenjis kenjis force-pushed the feat-closure-validation-rule branch from 24fd69c to 76c0e24 Compare December 12, 2022 01:10
@kenjis
Copy link
Member Author

kenjis commented Dec 12, 2022

@datamweb The &$error way does not support label. Because the $error value is directly assigned as the error message.

If you want to modify the value, it is another new feature.

You can use label with this way: 76c0e24

@kenjis kenjis force-pushed the feat-closure-validation-rule branch from 76c0e24 to 02038b9 Compare December 12, 2022 01:28
Copy link
Contributor

@datamweb datamweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kenjis , great👍.

@kenjis kenjis merged commit 97210e5 into codeigniter4:4.3 Dec 12, 2022
@kenjis kenjis deleted the feat-closure-validation-rule branch December 12, 2022 01:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.3 enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants