6

I have a plugin that needs to perform some custom validation on one of its settings. How would I go about doing that?

7

The gist is, you will need to give your plugin a dedicated Settings model class, where you will have complete control over its validation rules and errors.

Here’s how to set that up:

  1. Create a new Model class whose defineAttributes() method is based on your plugin's current defineSettings() method.

    class Foo_SettingsModel extends BaseModel
    {
        protected function defineAttributes()
        {
            return array(
                'settingName' => AttributeType::String,
                // ...
            );
        }
    }
    
  2. Give your plugin a getSettingsModel() function (instead of defineSettings()), which will return a new instance of your settings model.

    class FooPlugin extends BasePlugin
    {
        // ...
    
        protected function getSettingsModel()
        {
            return new Foo_SettingsModel();
        }
    }
    

To perform the actual validation, you have two options:

  1. Create a custom validator class (see the ones in craft/app/validators/ for examples), and make your plugin’s rules() method include it:

    class FooPlugin extends BasePlugin
    {
        // ...
    
        public function rules()
        {
            $rules = parent::rules();
            $rules[] = array('settingName', 'Craft\Foo_CustomValidatorClassName');
    
            return $rules;
        }
    }
    
  2. Override your plugin’s validate() method and just perform the validation directly from there:

    class FooPlugin extends BasePlugin
    {
        // ...
    
        public function validate($attributes = null, $clearErrors = true)
        {
            $validates = parent::validate($attributes, $clearErrors);
    
            if ($attributes === null || in_array('settingName', $attributes))
            {
                // Perform custom validation here
                // ...
    
                if (!$settingNameValidates)
                {
                    $this->addError('settingName', 'Error message');
                    $validates = false;
                }
            }
    
            return $validates;
        }
    }
    

Finally, the last thing you’ll need to ensure is that your plugin’s settings template is actually showing the error messages associated with this setting:

{{ forms.textField({
    ...
    errors: info.getErrors('settingName')
}) }}
| improve this answer | |
  • I am trying to do this for a fieldtype that matches input against a regex (PlainTextFieldType plus input has to match the regex). It has a function validate which checks that the input matches the regex when you try to save an entry with this field type. Now I need a new validation to check that the regex to match against is valid, which should prevent you from saving the field. Does that use the same function validate? – Marion Newlevant Jun 16 '15 at 21:28
  • I'm closer. There are two validate functions. The one in ...FieldType.php that validates entries with this field type, and the one in ...SettingsModel.php that validates the actual field type. – Marion Newlevant Jun 17 '15 at 4:07
  • @MarionNewlevant If you're still having trouble, it's probably worth posting a new question about it. – Brandon Kelly Jun 24 '15 at 16:17
  • I had to change extends Plugin to extends BasePlugin and extends Model to extends BaseModel to get this to work - otherwise I was getting errors. – Phil Gyford Jan 18 '16 at 9:34

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.