<p>The solution provided <a href="https://craftcms.stackexchange.com/questions/10162/how-can-i-add-custom-validation-rules-to-my-plugin-settings">here</a> actually required some adjustments. Here is what ended up working:</p>

<p>In <code>craft/plugins/sample/SamplePlugin.php</code>:</p>

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

class SamplePlugin extends BasePlugin {

    function getName() { ... }
    function getVersion() { ... }
    function getDeveloper() { ... }
    function getDeveloperUrl() { ... }

    public function getSettingsHtml()
    {
        return craft()-&gt;templates-&gt;render('sample/settings', [
            'settings' =&gt; $this-&gt;getSettings()
        ]);
    }

    protected function getSettingsModel()
    {
        return new Sample_SettingsModel();
    }

}
</code></pre>

<p>In <code>craft/plugins/sample/models/Sample_SettingsModel.php</code>:</p>

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

class Sample_SettingsModel extends BaseModel {

    protected function defineAttributes()
    {
        return [
            'mySetting' =&gt; [
                AttributeType::String,
                'required' =&gt; true
            ]
        ];
    }

    public function rules()
    {
        $rules = parent::rules();
        $rules[] = ['mySetting', 'validateMySetting'];

        return $rules;
    }

    public function validateMySetting($attribute)
    {
        $value = $this-&gt;$attribute;

        if ( /* validation condition */) {
            $message = Craft::t("Error message");
            $this-&gt;addError($attribute, $message);
        }
    }

}
</code></pre>

CC BY-SA 3.0
2016-01-18T09:33:40.640Z
Thanks for this - this worked for me, while the other thread you linked to didn't. A difference between Plugin/BasePlugin and Model/BaseModel I think.
Phil Gyford
CC BY-SA 3.0
2015-07-28T18:26:11.270Z
2015-07-28T18:26:11.270Z
2017-04-13T12:50:59.930Z
Garrett
<p>As a follow-up to <a href="https://craftcms.stackexchange.com/questions/5067/how-to-use-plugin-settings">this</a> thread, I would like to know how to create custom validation rules for plugin settings, or at least how to return field errors to the plugin settings view. I currently have:</p>

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

class SamplePlugin extends BasePlugin {

    function getName() { ... }
    function getVersion() { ... }
    function getDeveloper() { ... }
    function getDeveloperUrl() { ... }

    public function getSettingsHtml()
    {
        return craft()-&gt;templates-&gt;render('sample/settings', [
            'settings' =&gt; $this-&gt;getSettings()
        ]);
    }

    public function prepSettings($settings)
    {
        // I'm currently doing my 'custom' validation here. But if
        // there's an error, how do I display errors on the settings page?

        return $settings;
    }

    public function rules()
    {
        // Tried this, but since SamplePlugin isn't a Model, it does nothing.
    }

    protected function defineSettings()
    {
        return [
            'mySetting' =&gt; [
                AttributeType::String,
                'required' =&gt; true,
                // Can custom validation rules be set here?
            ]
        ];
    }

}
</code></pre>

CC BY-SA 3.0
2015-07-28T17:54:40.243Z
Duh. `$this->settings` _is_ the plugin settings model. That get's me going in the right direction.
Garrett
CC BY-SA 3.0
2015-07-28T18:01:45.460Z
Already asked/answered in this [thread](http://craftcms.stackexchange.com/questions/10162/how-can-i-add-custom-validation-rules-to-my-plugin-settings).
Garrett
CC BY-SA 3.0
2015-07-28T18:02:41.373Z
So, you're all set, then? Can we mark this one as a duplicate?
Brad Bell
CC BY-SA 3.0
2015-07-28T22:33:37.647Z
It is a duplicate question, however, the gist provided on the other post didn't work. I'm not sure if it's outdated or what. What I provided below is what actually worked for me.
Garrett
<plugin-development><settings>
Custom validation for plugin settings
# Custom validation for plugin settings

(Asked by: username_0 on 2015-07-28)

As a follow-up to this thread, I would like to know how to create custom validation rules for plugin settings, or at least how to return field errors to the plugin settings view. I currently have:
templates->render('sample/settings', [
            'settings' => $this->getSettings()
        ]);
    }

    public function prepSettings($settings)
    {
        // I'm currently doing my 'custom' validation here. But if
        // there's an error, how do I display errors on the settings page?

        return $settings;
    }

    public function rules()
    {
        // Tried this, but since SamplePlugin isn't a Model, it does nothing.
    }

    protected function defineSettings()
    {
        return [
            'mySetting' => [
                AttributeType::String,
                'required' => true,
                // Can custom validation rules be set here?
            ]
        ];
    }

}

**Tags:** plugin-development, settings

> **Comments:**
> **username_0**: Duh. `$this->settings` _is_ the plugin settings model. That get's me going in the right direction.  
> **username_0**: Already asked/answered in this [thread](http://craftcms.stackexchange.com/questions/10162/how-can-i-add-custom-validation-rules-to-my-plugin-settings).  
> **username_1**: So, you're all set, then? Can we mark this one as a duplicate?  
> **username_0**: It is a duplicate question, however, the gist provided on the other post didn't work. I'm not sure if it's outdated or what. What I provided below is what actually worked for me.  

---

## 1 answers

### Answer by username_0 on 2015-07-28. Score: 3 | Accepted answer

The solution provided here actually required some adjustments. Here is what ended up working:
In craft/plugins/sample/SamplePlugin.php:
templates->render('sample/settings', [
            'settings' => $this->getSettings()
        ]);
    }

    protected function getSettingsModel()
    {
        return new Sample_SettingsModel();
    }

}

In craft/plugins/sample/models/Sample_SettingsModel.php:
 [
                AttributeType::String,
                'required' => true
            ]
        ];
    }

    public function rules()
    {
        $rules = parent::rules();
        $rules[] = ['mySetting', 'validateMySetting'];

        return $rules;
    }

    public function validateMySetting($attribute)
    {
        $value = $this->$attribute;

        if ( /* validation condition */) {
            $message = Craft::t("Error message");
            $this->addError($attribute, $message);
        }
    }

}

> **Comments:**
> **username_2**: Thanks for this - this worked for me, while the other thread you linked to didn't. A difference between Plugin/BasePlugin and Model/BaseModel I think.  

---

