<p>You need both because there is no way to define custom validation rules from <code>defineAttributes()</code>.</p>

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

<pre><code>$rules = parent::rules();
$rules[] = ['email', 'Craft\MyPlugin_EmailValidator'];
</code></pre>

CC BY-SA 3.0
2015-04-22T02:48:32.693Z
That makes perfect sense, I was more curious if I was re-inventing the wheel by including the `rules` call in addition to the `defineAttributes` method.  Thank you!
Damon
CC BY-SA 3.0
2015-04-22T00:55:28.443Z
2015-04-22T00:55:28.443Z
Brad Bell
<p><a href="https://experiencehq.net/blog/the-definitive-guide-to-validation-in-craft-part-5-custom-validators" rel="nofollow">Using this fantastic article on validation,</a> I have written a custom validator class. Everything is working, but I'm curious about the <code>rules</code> and <code>defineAttributes</code> method(s). Most likely I'm just misunderstanding how it all works.</p>

<p><strong>MyModel.php</strong></p>

<pre><code>    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;
    }
</code></pre>

<p>Do I really need both? I (thought) that <code>defineAttributes</code> was the <code>rules</code> 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.</p>

<p>Thank you!</p>

<plugin-development><validation>
defineAttributes and rules
# defineAttributes and rules

(Asked by: username_0 on 2015-04-21)

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' => array(
                'type' => AttributeType::Name,
                'required' => true
            ),
            'lastName' => array(
                'type' => AttributeType::Name,
                'required' => true
            ),
            'email' => array(
                'type' => AttributeType::Email,
                'required' => true
            ),
            'phone' => array(
                'type' => AttributeType::ClassName,
                'maxLength' => 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!

**Tags:** plugin-development, validation


---

## 1 answers

### Answer by username_1 on 2015-04-22. Score: 0 | Accepted answer

You need both because there is no way to define custom validation rules from defineAttributes().
Ultimately things like 'maxLength' => 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'];

> **Comments:**
> **username_0**: That makes perfect sense, I was more curious if I was re-inventing the wheel by including the `rules` call in addition to the `defineAttributes` method.  Thank you!  

---

