defineAttributes and rules Using this fantastic article on validation, I have written a custom validator class. Everything is working, but I'm curious about the rules and defineAttributes method(s). Most likely I'm just misunderstanding how it all works.

MyModel.php

    public function defineAttributes()
    {
        return array(
            'firstName' =&gt; array(
                'type' =&gt; AttributeType::Name,
                'required' =&gt; true
            ),
            'lastName' =&gt; array(
                'type' =&gt; AttributeType::Name,
                'required' =&gt; true
            ),
            'email' =&gt; array(
                'type' =&gt; AttributeType::Email,
                'required' =&gt; true
            ),
            'phone' =&gt; array(
                'type' =&gt; AttributeType::ClassName,
                'maxLength' =&gt; 14
            )
        );
    }

    public function rules()
    {
        $rules = parent::rules();

        $rules[] = ['email', 'Craft\MyPlugin_EmailValidator'];

        return $rules;
    }


Do I really need both? I (thought) that defineAttributes was the rules method. Everything is working properly; I'm doing a check to make sure the email is unique. I have a unique key defined on the record as well.

Thank you!
You need both because there is no way to define custom validation rules from defineAttributes().

Ultimately things like 'maxLength' =&gt; 14 in defineAttributes() automagically get normalized down to the $rules array that gets returned from the rules method, so you're just appending your custom rule onto that when you call:

$rules = parent::rules();
$rules[] = ['email', 'Craft\MyPlugin_EmailValidator'];