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

Approach to extending identityCard rule #1465

Open
emrahoruc opened this issue Aug 16, 2024 · 2 comments
Open

Approach to extending identityCard rule #1465

emrahoruc opened this issue Aug 16, 2024 · 2 comments

Comments

@emrahoruc
Copy link

v::identityCard(string $countryCode)
https://respect-validation.readthedocs.io/en/1.1/rules/IdentityCard/

The identityCard rule currently only accepts verifying the Polish Identity Card numbers.
I added my own rule.
But... I couldn't add the classes to my own namespace(MyApp), so I added them to composer.json to load them.

Is my approach correct?

# src/Libraries/Validation/Rules/Locale/XxIdentityCard.php
<?php

namespace Respect\Validation\Rules\Locale;

use Respect\Validation\Rules\AbstractRule;

class XxIdentityCard extends AbstractRule
{
    public function validate($input)
    {
        // codes...
        return true;
    }
}
# src/Libraries/Validation/Exceptions/Locale/XxIdentityCardException.php
<?php

namespace Respect\Validation\Exceptions\Locale;

use Respect\Validation\Exceptions\ValidationException;

class XxIdentityCardException extends ValidationException
{
    /**
     * @var array
     */
    public static $defaultTemplates = [
        self::MODE_DEFAULT => [
            self::STANDARD => '{{name}} must be a valid Xx Identity Card number',
        ],
        self::MODE_NEGATIVE => [
            self::STANDARD => '{{name}} must not be a valid Xx Identity Card number',
        ],
    ];
}
# composer.json
...
"autoload": {
        "psr-4": {
          "MyApp\\": "src/",
          "Respect\\Validation\\Rules\\Locale\\": "src/Libraries/Validation/Rules/Locale/",
          "Respect\\Validation\\Exceptions\\Locale\\": "src/Libraries/Validation/Exceptions/Locale/"
        }
    },
...

Then composer dump-autoload

@andus4n
Copy link

andus4n commented Aug 31, 2024

if i understood correctly, you just want to add a new rule. on version 2.2.x, i'm doin' this in order to create "extra" rules:
\Respect\Validation\Factory::setDefaultInstance((new \Respect\Validation\Factory())->withRuleNamespace('\\Validators\\Respect\\Rules')->withExceptionNamespace('\\Validators\\Respect\\Exceptions'));

Validators\Respect\Rules -> the directory where the rule class will be
Validators\Respect\Exceptions -> the directory where the exception class will be

@emrahoruc
Copy link
Author

Hi @andus4n, first of all thank you for your response.

I already have extra rules with v::with('MyApp\\Libraries\\Validation\\Rules\\'); in version 1.1.

I don't think I'll be able to set the namespace in my XxIdentityCard class to MyApp due to the approach outlined below.
https://github.com/Respect/Validation/blob/1.1/library/Rules/IdentityCard.php#L23

        $className = __NAMESPACE__.'\\Locale\\'.$shortName;
        if (!class_exists($className)) {
            throw new ComponentException(sprintf('There is no support for identity cards from "%s"', $countryCode));
        }

Alternatively, I could create my own IdentityCard class and define the Locale classes within my own namespace.

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