<p>Yii/Active Record won't do this by default.  You'd need to manually add an <a href="http://www.yiiframework.com/doc/api/1.1/CExistValidator" rel="nofollow">exists</a> validation rule to the <code>typeId</code> attribute to get it to behave like you're looking for.</p>

CC BY-SA 3.0
2016-09-05T10:42:12.880Z
Thanks Brad. I have that working now. If I wanted to add the code to this the question to help others, what's the etiquette? Do I update my question or add my own answer?
Jamie Pittock
CC BY-SA 3.0
2016-09-05T14:31:15.973Z
@JamiePittock I'd go ahead and request that Brad update his answer to include more details and add your own answer with the example that worked for you.  As a reader, I'd vote up the answer with an explicit example and like the separation of question and answer so answers can be voted up independently.
Ben Parizek
CC BY-SA 3.0
2016-09-05T15:19:13.730Z
@BenParizek Yeah, on mobile. I can update it when I'm back at a real computer or feel free to add a separate answer Jamie because there's a 90% chance I'll forget, too.
Brad Bell
CC BY-SA 3.0
2016-09-05T16:24:09.177Z
Thanks guys. I've posted a separate answer. It was a bit trial and error to figure out but hopefully it'll help the next person looking.
Jamie Pittock
CC BY-SA 3.0
2016-09-05T04:43:21.283Z
2016-09-05T04:43:21.283Z
Brad Bell
<p>Thanks to Brad for pointing me in the right direction.</p>

<p>I added this <code>rules</code> method to my record.</p>

<pre><code>public function rules()
{
   $rules = parent::rules();

   $rules[] = [
       'typeId',
       'exist',
       'allowEmpty' =&gt; false,
       'attributeName' =&gt; 'id',
       'className' =&gt; 'Craft\Events_EventTypeRecord',
       'message' =&gt; 'You must enter a valid type'
   ];

   return $rules;
}
</code></pre>

<p><code>exist</code> is <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/framework/validators/CValidator.php#L64" rel="nofollow">a shortcut</a> to Yii's built-in <a href="http://www.yiiframework.com/doc/api/1.1/CExistValidator" rel="nofollow">CExistValidator</a> validator. The other key => value pairs are properties of that built-in validator.</p>

<p>You could also do it as a separate validator class. Something like:</p>

<pre><code>&lt;?php namespace Craft;

use CExistValidator;

class Events_EventTypeExistValidator extends CExistValidator
{

    public function validateAttribute($object,$attribute)
    {
        $this-&gt;message = "You must enter a type";
        $this-&gt;allowEmpty = false;
        $this-&gt;attributeName = 'id';
        $this-&gt;className = 'Craft\Events_EventTypeRecord';

        parent::validateAttribute($object,$attribute);
    }
}
</code></pre>

<p>If you did it this way your record's <code>rules</code> method would include:</p>

<pre><code>public function rules()
{
   $rules = parent::rules();

   $rules[] = ['typeId', 'Craft\Events_EventTypeExistValidator'];

   return $rules;
}
</code></pre>

CC BY-SA 3.0
2016-09-05T16:21:45.100Z
2016-09-05T16:21:45.100Z
Jamie Pittock
<p>Say I have a record defining a foreign key relationship which is set to required, such as:  </p>

<pre><code>public function defineRelations()
{
    return [
        'type' =&gt; [
            static::BELONGS_TO,
            'Events_EventTypeRecord',
            'required' =&gt; true,
            'onDelete' =&gt; static::CASCADE
        ]
    ];
}
</code></pre>

<p>If I try to save the record with <code>typeId</code> not set I'll eventually get a database exception but the error won't show up if I call <code>$record-&gt;validate()</code>.</p>

<p>How do I make a relationship fail validation?</p>

<relations><validation><records>
How do I include record relationships in validation?
# How do I include record relationships in validation?

(Asked by: username_0 on 2016-09-04)

Say I have a record defining a foreign key relationship which is set to required, such as:  
public function defineRelations()
{
    return [
        'type' => [
            static::BELONGS_TO,
            'Events_EventTypeRecord',
            'required' => true,
            'onDelete' => static::CASCADE
        ]
    ];
}

If I try to save the record with typeId not set I'll eventually get a database exception but the error won't show up if I call $record->validate().
How do I make a relationship fail validation?

**Tags:** relations, validation, records

---

## 2 answers

### Answer by username_1 on 2016-09-05. Score: 3 | Accepted answer

Yii/Active Record won't do this by default. You'd need to manually add an [exists](http://www.yiiframework.com/doc/api/1.1/CExistValidator) validation rule to the `typeId` attribute to get it to behave like you're looking for.

> **Comments:**
> **username_0**: Thanks Brad. I have that working now. If I wanted to add the code to this the question to help others, what's the etiquette? Do I update my question or add my own answer?  
> **username_2**: @JamiePittock I'd go ahead and request that Brad update his answer to include more details and add your own answer with the example that worked for you.  As a reader, I'd vote up the answer with an explicit example and like the separation of question and answer so answers can be voted up independently.  
> **username_1**: @BenParizek Yeah, on mobile. I can update it when I'm back at a real computer or feel free to add a separate answer Jamie because there's a 90% chance I'll forget, too.  
> **username_0**: Thanks guys. I've posted a separate answer. It was a bit trial and error to figure out but hopefully it'll help the next person looking.  

---

### Answer by username_0 on 2016-09-05. Score: 2

Thanks to Brad for pointing me in the right direction.
I added this rules method to my record.
public function rules()
{
   $rules = parent::rules();

   $rules[] = [
       'typeId',
       'exist',
       'allowEmpty' => false,
       'attributeName' => 'id',
       'className' => 'Craft\Events_EventTypeRecord',
       'message' => 'You must enter a valid type'
   ];

   return $rules;
}

exist is a shortcut to Yii's built-in CExistValidator validator. The other key => value pairs are properties of that built-in validator.
You could also do it as a separate validator class. Something like:
message = "You must enter a type";
        $this->allowEmpty = false;
        $this->attributeName = 'id';
        $this->className = 'Craft\Events_EventTypeRecord';

        parent::validateAttribute($object,$attribute);
    }
}

If you did it this way your record's rules method would include:
public function rules()
{
   $rules = parent::rules();

   $rules[] = ['typeId', 'Craft\Events_EventTypeExistValidator'];

   return $rules;
}


---

