rynpsc / craft-phone-number

Phone Number field for Craft CMS.
https://plugins.craftcms.com/phone-number
MIT License
22 stars 3 forks source link

Model validation rules #23

Closed stanislavprokopov closed 3 months ago

stanislavprokopov commented 3 months ago

I need to add some custom validation rules for the phone number field, was trying to do it using EVENT_DEFINE_RULES model event, but it does not work, because the PhoneNumberModel class overrides the public function rules(): array method, any reason this is done instead of defining rules in protected function defineRules(): array?

Event::on(
    \rynpsc\phonenumber\models\PhoneNumberModel::class,
    \rynpsc\phonenumber\models\PhoneNumberModel::EVENT_DEFINE_RULES,
    function (DefineRulesEvent $event) {
        $event->rules[] = [['number'], CustomPhoneNumberValidator::class];
    }
);
stanislavprokopov commented 3 months ago

Also it would be great if you could add an event to getElementValidationRules() function in PhoneNumberField class, allowing to add additional validators, the defineRules event will not be enough to trigger required custom validation in all cases.

rynpsc commented 3 months ago

I need to add some custom validation rules for the phone number field, was trying to do it using EVENT_DEFINE_RULES model event, but it does not work, because the PhoneNumberModel class overrides the public function rules(): array method, any reason this is done instead of defining rules in protected function defineRules(): array?

Nope, defineRules was added in Craft 3.40, this plugin predates that, when the only option as via rules. It escaped me to check this post Craft 3.4.0.

Thanks for flagging. I'll look at switching to defineRules.

Also it would be great if you could add an event to getElementValidationRules() function in PhoneNumberField class, allowing to add additional validators, the defineRules event will not be enough to trigger required custom validation in all cases.

I don't think I'll add an event as none of the native Craft fields do so. The Craft docs have an example of how to add validation to fields in Elements which I assume is what you'll want to do?

https://craftcms.com/docs/4.x/extend/events.html#adding-validation-rules (The same works in Craft 5)

For this field type, using an inline handler this would look something like:

use yii\base\Event;
use craft\elements\User;
use craft\events\DefineRulesEvent;

Event::on(
    Entry::class,
    Entry::EVENT_DEFINE_RULES,
    function(DefineRulesEvent $event) {
        /** @var Entry $entry */
        $entry = $event->sender;

        $event->rules[] = ['field:phone', function ($attribute, $params, $validator) use ($entry) {
            $model = $entry->$attribute;

            if (strlen($model->number) <= 100) {
                $entry->addError($attribute, 'Number must be at least 100 characters long');
            }
        }];
    }
);
stanislavprokopov commented 3 months ago

Missed the part regarding validation to fields in Elements in Craft docs, thanks for the hint, that solved it for me.