Craft 3 Custom Plugin: model validation Struggling a bit here - have created a custom plugin where I want to validate form submission against a predefined model before saving to the database. All pretty straightforward so far …

However, if I try to use native (Yii 2) validation then a record is saved regardless of whether it is valid or not. For example, if I set the model rules as follows:

public function rules()
    {
        return [
            [['formName'], 'required'],
            [['formName'], 'string', 'min' =&gt; 1, 'max' =&gt; 255, 'skipOnEmpty' =&gt; false]
        ];
    }


The record is saved, even if $model-&gt;formName is empty. For example, see the following Controller code:

public function actionUpdate()
    {
        $this-&gt;requirePostRequest();
        $request = Craft::$app-&gt;getRequest();
        $site = Craft::$app-&gt;getSites()-&gt;getCurrentSite();   

        $formName = $request-&gt;getBodyParam('formName');
        $formId = $request-&gt;getBodyParam('formId');

        // get ID (if exists)

        if ($formId) {
            $formRecord = FormRecord::find()
            -&gt;where(['id'=&gt;$formId])-&gt;one();
            // if count == null return false
        } else {
            $formRecord = new FormRecord;
        }

        $formRecord-&gt;formName = $formName;
        $formRecord-&gt;siteId = $site-&gt;id;
        if ($formRecord-&gt;validate()) {
        …


The code will always validate - even if the formName is empty.
This seems a bit crazy to me - am I misunderstanding something about Yii validation rules here? 

Help/thoughts appreciated,

Cole
I am a beginner at craft cms,but  I think these would help you:-
1.install the plugin guest entries and look at their code
2.look at this article