Provide validation for minimum number inserted in Yii2
<p>i am having trouble with this validation, </p>

<p>how can i validate minimum number to be inserted in </p>

<pre><code>$form-&gt;field($modeldetails, "[{$i}]qty")-&gt;textInput()
</code></pre>

<p>based on sum of table quantity field with parameter to filter from <code>$form-&gt;field($model, 'tgl')</code> because i cannot simply add range validation in models rule. </p>

<p>I need a function to get paramater from two models <code>($model,$modeldetails)</code>, and process summary of quantity, if user inserted more than minimum sum then show an error in quantity field.</p>

<p><strong>View activeform field for header</strong> ( using dynamic form )</p>

<pre><code>&lt;?php $form = ActiveForm::begin(['id' =&gt; 'dynamic-form')]); ?&gt;


        &lt;?= $form-&gt;field($model, 'tgl')-&gt;widget(DatePicker::classname(), [
                'options' =&gt; ['placeholder' =&gt; 'Enter Transaction Date ...'],
                'type' =&gt; DatePicker::TYPE_COMPONENT_APPEND,
                'pluginOptions' =&gt; [
                    'autoclose'=&gt;true,
                    'format' =&gt; 'yyyy-mm-dd',
                    'todayHighlight' =&gt; true
                ],

            ]); ?&gt;
</code></pre>

<p><strong>View activeform field for detail</strong> </p>

<pre><code>&lt;?php DynamicFormWidget::begin([
            'widgetContainer' =&gt; 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
            'widgetBody' =&gt; '.container-items', // required: css class selector
            'widgetItem' =&gt; '.item', // required: css class
            'limit' =&gt; 15, // the maximum times, an element can be cloned (default 999)
            'min' =&gt; 1, // 0 or 1 (default 1)
            'insertButton' =&gt; '.add-item', // css class
            'deleteButton' =&gt; '.remove-item', // css class
            'model' =&gt; $modeldetail[0],
            'formId' =&gt; 'dynamic-form',
            'formFields' =&gt; [
                'id_brg',
                'qty',

            ],
        ]); ?&gt;

    &lt;div class="panel panel-default"&gt;
        &lt;div class="panel-heading"&gt;
            &lt;h4&gt;
                Detail
                &lt;button type="button" class="add-item btn btn-success btn-sm pull-right"&gt;&lt;i class="glyphicon glyphicon-plus"&gt;&lt;/i&gt; Add&lt;/button&gt;
            &lt;/h4&gt;
        &lt;/div&gt;
        &lt;div class="panel-body"&gt;
            &lt;div class="container-items"&gt;&lt;!-- widgetBody --&gt;
            &lt;?php foreach ($modeldetail as $i =&gt; $modeldetails): ?&gt;
                &lt;div class="item panel panel-default"&gt;&lt;!-- widgetItem --&gt;
                    &lt;div class="panel-heading"&gt;
                        &lt;h3 class="panel-title pull-left"&gt;&lt;/h3&gt;
                        &lt;div class="pull-right"&gt;
                            &lt;button type="button" class="remove-item btn btn-danger btn-xs"&gt;&lt;i class="glyphicon glyphicon-minus"&gt;&lt;/i&gt;&lt;/button&gt;
                        &lt;/div&gt;
                        &lt;div class="clearfix"&gt;&lt;/div&gt;
                    &lt;/div&gt;
                    &lt;div class="panel-body"&gt;
                        &lt;?php
                            // necessary for update action.
                            if (! $modeldetails-&gt;isNewRecord) {
                                echo Html::activeHiddenInput($modeldetails, "[{$i}]id");
                            }
                        ?&gt;

                        &lt;div class="row"&gt;
                            &lt;div class="col-sm-6"&gt;
                                &lt;?= $form-&gt;field($modeldetails, "[{$i}]id_brg")-&gt;label('Nama')-&gt;widget(Select2::classname(), [
                                        'data' =&gt; $brg,
                                        'language' =&gt; 'en',
                                        'options' =&gt; ['placeholder' =&gt; 'Select an item ...'],
                                        'pluginOptions' =&gt; [
                                            'allowClear' =&gt; true
                                        ],
                                    ]);?&gt;
                            &lt;/div&gt;
                            &lt;div class="col-sm-6"&gt;
                                &lt;?= $form-&gt;field($modeldetails, "[{$i}]qty")-&gt;textInput() ?&gt; 
                            &lt;/div&gt;
                        &lt;/div&gt;&lt;!-- .row --&gt;

                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;?php endforeach; ?&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;&lt;!-- .panel --&gt;
&lt;?php DynamicFormWidget::end(); ?&gt;
</code></pre>

<p><strong>my models for details</strong></p>

<pre><code>class Detail extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'detail';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id_brg', 'qty'], 'required'],
            [['kode', 'id_brg', 'qty'], 'integer'],

        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' =&gt; 'ID',
            'kode' =&gt; 'Kode',
            'id_brg' =&gt; 'ID Barang',
            'qty' =&gt; 'Qty',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getLinkheader()
    {
        return $this-&gt;hasOne(Transaksi::className(), ['kode' =&gt; 'kode']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getItems()
    {
        return $this-&gt;hasOne(Barang::className(), ['id' =&gt; 'id_brg']);
    }
}
</code></pre>