solspace / craft-calendar

Calendar for Craft: The most reliable and powerful event management plugin for Craft.
http://docs.solspace.com/craft/calendar/v5
Other
15 stars 16 forks source link

Calendar Model doesn't work with yii Model Events #141

Closed myleshyson closed 2 years ago

myleshyson commented 2 years ago

Trying to add a validation rule to the CalendarModel. The rules function in that class doesn't call parent::rules(), so the EVENT_DEFINE_RULES event doesn't work like it should.

Any chance you can update that function to look something like this?

 public function rules(): array
    {
        $rules = parent::rules();
        $rules[] = [
            ['titleFormat'],
            'required',
            'when' => function (self $model) {
                return !$model->hasTitleField;
            }
        ];
        $rules[] = [
            ['titleLabel'],
            'required',
            'when' => function (self $model) {
                return $model->hasTitleField;
            }
        ];

        return $rules;
    }

Or instead of using the rules method, maybe use the defineRules method?

protected function defineRules(): array
    {
        $rules = parent::defineRules();
        $rules[] = [
            ['titleFormat'],
            'required',
            'when' => function (self $model) {
                return !$model->hasTitleField;
            }
        ];
        $rules[] = [
            ['titleLabel'],
            'required',
            'when' => function (self $model) {
                return $model->hasTitleField;
            }
        ];

        return $rules;
    }

defineRules seems to be how core craft models define their validation rulesets, I think so that people can extend if needed.

kjmartens commented 2 years ago

This is now fixed in Calendar 3.13.13 and 4.0.0-beta.6. 🙂