nystudio107 / generator-craftplugin

generator-craftplugin is a Yeoman generator for Craft CMS plugins
MIT License
73 stars 30 forks source link

Missing parent::rules() for ElementTypes #102

Open jerome2710 opened 4 years ago

jerome2710 commented 4 years ago

I was having a really hard time figuring out why my custom ElementType would not save. Figured out the generated element overrides the public function rules() of the \yii\base\Model::validate, but does not call the parent::rules().

Exception thrown was: [error][yii\base\InvalidArgumentException] yii\base\InvalidArgumentException: Unknown scenario: essentials in [redacted]/vendor/yiisoft/yii2/base/Model.php:357

Generated \modules\somemodule\elements\CustomElement::rules was:

public function rules()
{
    return [
        ['someAttribute', 'string'],
        ['someAttribute', 'default', 'value' => 'Some Default'],
    ];
}

should be:

public function rules()
{
    $rules = parent::rules();

    $rules[] = ['someAttribute', 'string'];
    $rules[] = ['someAttribute', 'default', 'value' => 'Some Default'];

    return $rules;
}