<p>I had a similar situation a few years ago. I ended up solving it with a <code>DynamicModel</code>. Yii has this concept of creating ad-hoc validation without the need for a full-blown model. Here's the docs:  <a href="https://www.yiiframework.com/doc/guide/2.0/en/input-validation#ad-hoc-validation" rel="nofollow noreferrer">https://www.yiiframework.com/doc/guide/2.0/en/input-validation#ad-hoc-validation</a>.</p>
<p>I quite like it for non-dynamic use cases too, to be honest, it's quick and easy - not everything needs the formality and added complexity of a dedicated Model in my opinion.</p>

CC BY-SA 4.0
2024-10-23T14:41:23.143Z
2024-10-23T14:41:23.143Z
James Smith
<p>I have created a model in a module and have some fields that will always be there but there are some other fields which depend on other configuration.
Basically there is a posibillty to have 1 question, 20 questions or any in between.</p>
<p>I can add them to the rules but for some reason I can't add them as a public property.
The commented out $question_x properties are what I need but can't hardcode.</p>
<p>How can I make this work?</p>
<pre><code>&lt;?php

namespace modules\backend\models;

use Craft;
use craft\base\Model;

/**
 * Survey model
 */
class Survey extends Model
{
    public $firstName;
    public $lastName;
    public $email;
    // public $question_0;
    // public $question_1;
    // public $question_2;
    // public $question_3;

    // Property to hold the entry containing the questions
    public $questionEntry;

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

        // Add the base rules
        $rules[] = [['firstName', 'lastName', 'email'], 'required'];
        $rules[] = [['email'], 'email'];

        // Dynamically add rules for the question fields
        if ($this-&gt;questionEntry) {
            // Assuming the questions are stored as an array in a custom field of the entry
            $questions = $this-&gt;questionEntry-&gt;surveyQuestions-&gt;all() ?? [];

            // Loop through the questions and add required validation for each corresponding field
            foreach ($questions as $index =&gt; $question) {
                $questionField = 'question_' . $index;

                $propertyName = 'question_' . $index;
                $this-&gt;$propertyName = $this-&gt;$propertyName ?? null; // Dynamically assign to the class instance

                // Add required rule for each question field
                $rules[] = [[$questionField], 'required', 'message' =&gt; 'Question ' . $index . ' is required.'];
            }
        }

        return $rules;
    }
}

</code></pre>

<plugin-development><modules>
Add properties dynamically to model
# Add properties dynamically to model

(Asked by: username_0 on 2024-10-23)

I have created a model in a module and have some fields that will always be there but there are some other fields which depend on other configuration.
Basically there is a posibillty to have 1 question, 20 questions or any in between.
I can add them to the rules but for some reason I can't add them as a public property.
The commented out $question_x properties are what I need but can't hardcode.
How can I make this work?
questionEntry) {
            // Assuming the questions are stored as an array in a custom field of the entry
            $questions = $this->questionEntry->surveyQuestions->all() ?? [];

            // Loop through the questions and add required validation for each corresponding field
            foreach ($questions as $index => $question) {
                $questionField = 'question_' . $index;

                $propertyName = 'question_' . $index;
                $this->$propertyName = $this->$propertyName ?? null; // Dynamically assign to the class instance

                // Add required rule for each question field
                $rules[] = [[$questionField], 'required', 'message' => 'Question ' . $index . ' is required.'];
            }
        }

        return $rules;
    }
}

**Tags:** plugin-development, modules

---

## 1 answers

### Answer by username_1 on 2024-10-23. Score: 1 | Accepted answer

I had a similar situation a few years ago. I ended up solving it with a `DynamicModel`. Yii has this concept of creating ad-hoc validation without the need for a full-blown model. Here's the docs: <https://www.yiiframework.com/doc/guide/2.0/en/input-validation#ad-hoc-validation>.

I quite like it for non-dynamic use cases too, to be honest, it's quick and easy - not everything needs the formality and added complexity of a dedicated Model in my opinion.


---

