Answers.Body: <p>Since Craft 3.4.0 you should be using <code>defineRules()</code> over <code>rules()</code> in your model classes.</p>
  <blockquote>
  <p>Models should override this method instead of [[rules()]] so [[EVENT_DEFINE_RULES]] handlers can modify the class-defined rules.</p>
  </blockquote>
  <p>See <a href="https://github.com/craftcms/cms/blob/d2b867f601cd75a33a22a016d8ea823bdda614e4/src/base/Model.php#L127-L141" rel="nofollow noreferrer">https://github.com/craftcms/cms/blob/d2b867f601cd75a33a22a016d8ea823bdda614e4/src/base/Model.php#L127-L141</a></p>
  <p>But that doesn't explain why your rules are not working. Be sure to place the <code>defineRules()</code> method on the record class.</p>
  <pre class="lang-php prettyprint-override"><code>use craft\db\ActiveRecord;
  
  class FormRecord extends ActiveRecord
  {
      public function defineRules()
      {
          return [
              [['formName'], 'required'],
              [['formName'], 'string', 'min' =&gt; 1, 'max' =&gt; 255],
          ];
      }
  }
  </code></pre>
  <p>Then if <code>$formRecord-&gt;formName</code> is empty, <code>$formRecord-&gt;validate()</code> should evaluate to <code>false</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-25T08:46:15.087Z
Answers.LastActivityDate: 2022-07-25T08:46:15.087Z
Answers.UserDisplayName: Ben Croker
Answers.Body: <p>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.<a href="https://medium.com/@mccallister/handling-model-validation-in-craft-cms-template-forms-with-plugins-25a7a2afe27b" rel="nofollow noreferrer">look at this article</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-28T19:04:34.593Z
Answers.LastActivityDate: 2020-03-28T19:04:34.593Z
Answers.UserDisplayName: jamesSeb
Body: <p>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 …</p>
  
  <p>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:</p>
  
  <pre><code>public function rules()
      {
          return [
              [['formName'], 'required'],
              [['formName'], 'string', 'min' =&gt; 1, 'max' =&gt; 255, 'skipOnEmpty' =&gt; false]
          ];
      }
  </code></pre>
  
  <p>The record is saved, even if <code>$model-&gt;formName</code> is empty. For example, see the following Controller code:</p>
  
  <pre><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()) {
          …
  </code></pre>
  
  <p>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? </p>
  
  <p>Help/thoughts appreciated,</p>
  
  <p>Cole</p>
  
Tags: <plugin-development><craft3><validation><yii>
Title: Craft 3 Custom Plugin: model validation
ThreadText: # Craft 3 Custom Plugin: model validation
  
  (Asked by: username_0 on 2018-08-13)
  
  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' => 1, 'max' => 255, 'skipOnEmpty' => false]
          ];
      }
  
  ```
  
  The record is saved, even if `$model->formName` is empty. For example, see the following Controller code:
  
  ```
  public function actionUpdate()
      {
          $this->requirePostRequest();
          $request = Craft::$app->getRequest();
          $site = Craft::$app->getSites()->getCurrentSite();   
  
          $formName = $request->getBodyParam('formName');
          $formId = $request->getBodyParam('formId');
  
          // get ID (if exists)
  
          if ($formId) {
              $formRecord = FormRecord::find()
              ->where(['id'=>$formId])->one();
              // if count == null return false
          } else {
              $formRecord = new FormRecord;
          }
  
          $formRecord->formName = $formName;
          $formRecord->siteId = $site->id;
          if ($formRecord->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,
  
  username_0
  
  **Tags:** plugin-development, craft3, validation, yii
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2022-07-25. Score: 1
  
  Since Craft 3.4.0 you should be using `defineRules()` over `rules()` in your model classes.
  
  > Models should override this method instead of [[rules()]] so [[EVENT\_DEFINE\_RULES]] handlers can modify the class-defined rules.
  
  See <https://github.com/craftcms/cms/blob/d2b867f601cd75a33a22a016d8ea823bdda614e4/src/base/Model.php#L127-L141>
  
  But that doesn't explain why your rules are not working. Be sure to place the `defineRules()` method on the record class.
  
  ```php
  use craft\db\ActiveRecord;
  
  class FormRecord extends ActiveRecord
  {
      public function defineRules()
      {
          return [
              [['formName'], 'required'],
              [['formName'], 'string', 'min' => 1, 'max' => 255],
          ];
      }
  }
  
  ```
  
  Then if `$formRecord->formName` is empty, `$formRecord->validate()` should evaluate to `false`.
  
  
  ---
  
  ### Answer by username_2 on 2020-03-28. Score: 0
  
  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](https://medium.com/@mccallister/handling-model-validation-in-craft-cms-template-forms-with-plugins-25a7a2afe27b)
  
  
  ---
  
  