Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-18T09:33:40.640Z
Answers.Text: 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.
Answers.UserDisplayName: Phil Gyford
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-28T18:26:11.270Z
Answers.LastActivityDate: 2015-07-28T18:26:11.270Z
Answers.LastEditDate: 2017-04-13T12:50:59.930Z
Answers.UserDisplayName: Garrett
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-28T17:54:40.243Z
Comments.Text: Duh. `$this->settings` _is_ the plugin settings model. That get's me going in the right direction.
Comments.UserDisplayName: Garrett
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-28T18:01:45.460Z
Comments.Text: Already asked/answered in this [thread](http://craftcms.stackexchange.com/questions/10162/how-can-i-add-custom-validation-rules-to-my-plugin-settings).
Comments.UserDisplayName: Garrett
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-28T18:02:41.373Z
Comments.Text: So, you're all set, then? Can we mark this one as a duplicate?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-28T22:33:37.647Z
Comments.Text: 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.
Comments.UserDisplayName: Garrett
Tags: <plugin-development><settings>
Title: Custom validation for plugin settings
ThreadText: # Custom validation for plugin settings
  
  (Asked by: username_0 on 2015-07-28)
  
  As a follow-up to [this](https://craftcms.stackexchange.com/questions/5067/how-to-use-plugin-settings) 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:
  
  ```
  <?php namespace Craft;
  
  class SamplePlugin extends BasePlugin {
  
      function getName() { ... }
      function getVersion() { ... }
      function getDeveloper() { ... }
      function getDeveloperUrl() { ... }
  
      public function getSettingsHtml()
      {
          return craft()->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](https://craftcms.stackexchange.com/questions/10162/how-can-i-add-custom-validation-rules-to-my-plugin-settings) actually required some adjustments. Here is what ended up working:
  
  In `craft/plugins/sample/SamplePlugin.php`:
  
  ```
  <?php namespace Craft;
  
  class SamplePlugin extends BasePlugin {
  
      function getName() { ... }
      function getVersion() { ... }
      function getDeveloper() { ... }
      function getDeveloperUrl() { ... }
  
      public function getSettingsHtml()
      {
          return craft()->templates->render('sample/settings', [
              'settings' => $this->getSettings()
          ]);
      }
  
      protected function getSettingsModel()
      {
          return new Sample_SettingsModel();
      }
  
  }
  
  ```
  
  In `craft/plugins/sample/models/Sample_SettingsModel.php`:
  
  ```
  <?php namespace Craft;
  
  class Sample_SettingsModel extends BaseModel {
  
      protected function defineAttributes()
      {
          return [
              'mySetting' => [
                  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.  
  
  ---
  
  