Validation icon indicating copy to clipboard operation
Validation copied to clipboard

Approach to extending identityCard rule

Open emrahoruc opened this issue 1 year ago • 2 comments

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

emrahoruc avatar Aug 16 '24 16:08 emrahoruc

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

andus4n avatar Aug 31 '24 15:08 andus4n

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.

emrahoruc avatar Sep 16 '24 17:09 emrahoruc

What I recommend you to do is to just call your rule directly, without going through the IdentityCard rule.

v::with('MyApp\\Libraries\\Validation\\Rules\\');

v::xxIdentityCard()->assert($input);

It would have been great if we can extend the IdentityCard, but sometime I even wonder if this rule should be part of the core library or a different package altogether.

I'm closing this because I think that's pretty much it, but feel free to reopen it if you'd like

henriquemoody avatar Dec 13 '24 11:12 henriquemoody