How do I validate an array model attribute against a set of possible values? I have a need to set a property on a plugin Model that is an array of one or more valid values.

I can see that I can use AttributeType::Enum and record and define a set of values but if I'm not mistaken, that enables me to validate a single value against that set of values:

class MyModel extends BaseModel
{
    protected function defineAttributes()
    {
        return [
            'mySetting' =&gt; [ AttributeType::Enum, 'values' =&gt; 'validOption1,validOption2,validOption3' ]
        ];
     }
}


What I want to do is enable mySetting to be an array that would be valid when set as an array containing any combination of the defined options:

MyModel::populateModel(['mySetting' =&gt; [
    'validOption1'
]]); // is valid

MyModel::populateModel(['mySetting' =&gt; [
    'validOption1', 
    'validOption3'
]]); // is valid

MyModel::populateModel(['mySetting' =&gt; [
    'invalidOption', 
    'validOption3'
]]); // is *not* valid

MyModel::populateModel(['mySetting' =&gt; []); // is *not* valid


Etc.
Found an answer in the form of a customer validator class on the model:

https://experiencehq.net/blog/the-definitive-guide-to-validation-in-craft-part-5-custom-validators#newsletter

So something like this:

    public function validateMySetting($attribute)
    {
        $values = (is_array($this-&gt;$attribute)) ? $this-&gt;$attribute : [ $this-&gt;$attribute ];

        $validValues = ['validOption1', 'validOption2', 'validOption3'];

        foreach($values as $value)
        {
            if ( ! in_array($value, $validValues))
            {
                $this-&gt;addError($attribute, "{$value} is not a valid setting");
            }
        }
    }


And then to call it, override the parent class' rules method:

public function rules ()
{
    return array_merge(parent::rules(), [ 
        'someAttributeName', 'validateMySetting'
    ];
}