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!