1

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:

<?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?
            ]
        ];
    }

}
  • Duh. $this->settings is the plugin settings model. That get's me going in the right direction. – Garrett Jul 28 '15 at 17:54
  • Already asked/answered in this thread. – Garrett Jul 28 '15 at 18:01
  • So, you're all set, then? Can we mark this one as a duplicate? – Brad Bell Jul 28 '15 at 18:02
  • 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 Jul 28 '15 at 22:33
3

The solution provided here 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);
        }
    }

}
  • 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 Jan 18 '16 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.