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:
<?php namespace Craft;
use CExistValidator;
class Events_EventTypeExistValidator extends CExistValidator
{
public function validateAttribute($object,$attribute)
{
$this->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;
}