<p>Found an answer in the form of a customer validator class on the model:</p>

<p><a href="https://experiencehq.net/blog/the-definitive-guide-to-validation-in-craft-part-5-custom-validators#newsletter" rel="nofollow">https://experiencehq.net/blog/the-definitive-guide-to-validation-in-craft-part-5-custom-validators#newsletter</a></p>

<p>So something like this:</p>

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

<p>And then to call it, override the parent class' <code>rules</code> method:</p>

<pre><code>public function rules ()
{
    return array_merge(parent::rules(), [ 
        'someAttributeName', 'validateMySetting'
    ];
}
</code></pre>

CC BY-SA 3.0
2015-10-21T07:15:39.413Z
It is not getting into my custom validator's callback function. How do I do that?
Volatil3
CC BY-SA 3.0
2015-10-23T06:45:21.967Z
@Volatil3 I've updated the answer with details of how to add your custom validator method to the model's validation rules.
Russ Back
CC BY-SA 3.0
2015-03-26T22:05:23.887Z
2015-10-23T06:44:45.613Z
2015-10-23T06:44:45.613Z
Russ Back
<p>I have a need to set a property on a plugin Model that is an array of one or more valid values.</p>

<p>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:</p>

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

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

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

<p>Etc.</p>

<plugin-development><models><attribute>
How do I validate an array model attribute against a set of possible values?
# How do I validate an array model attribute against a set of possible values?

(Asked by: username_0 on 2015-03-26)

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' => [ AttributeType::Enum, 'values' => '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' => [
    'validOption1'
]]); // is valid

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

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

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

Etc.

**Tags:** plugin-development, models, attribute


---

## 1 answers

### Answer by username_0 on 2015-03-26. Score: 4

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->$attribute)) ? $this->$attribute : [ $this->$attribute ];

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

        foreach($values as $value)
        {
            if ( ! in_array($value, $validValues))
            {
                $this->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'
    ];
}

> **Comments:**
> **username_1**: It is not getting into my custom validator's callback function. How do I do that?  
> **username_0**: @username_1 I've updated the answer with details of how to add your custom validator method to the model's validation rules.  

---

