Reputation: 21
i am having trouble with this validation,
how can i validate minimum number to be inserted in
$form->field($modeldetails, "[{$i}]qty")->textInput()
based on sum of table quantity field with parameter to filter from $form->field($model, 'tgl')
because i cannot simply add range validation in models rule.
I need a function to get paramater from two models ($model,$modeldetails)
, and process summary of quantity, if user inserted more than minimum sum then show an error in quantity field.
View activeform field for header ( using dynamic form )
<?php $form = ActiveForm::begin(['id' => 'dynamic-form')]); ?>
<?= $form->field($model, 'tgl')->widget(DatePicker::classname(), [
'options' => ['placeholder' => 'Enter Transaction Date ...'],
'type' => DatePicker::TYPE_COMPONENT_APPEND,
'pluginOptions' => [
'autoclose'=>true,
'format' => 'yyyy-mm-dd',
'todayHighlight' => true
],
]); ?>
View activeform field for detail
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 15, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modeldetail[0],
'formId' => 'dynamic-form',
'formFields' => [
'id_brg',
'qty',
],
]); ?>
<div class="panel panel-default">
<div class="panel-heading">
<h4>
Detail
<button type="button" class="add-item btn btn-success btn-sm pull-right"><i class="glyphicon glyphicon-plus"></i> Add</button>
</h4>
</div>
<div class="panel-body">
<div class="container-items"><!-- widgetBody -->
<?php foreach ($modeldetail as $i => $modeldetails): ?>
<div class="item panel panel-default"><!-- widgetItem -->
<div class="panel-heading">
<h3 class="panel-title pull-left"></h3>
<div class="pull-right">
<button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<?php
// necessary for update action.
if (! $modeldetails->isNewRecord) {
echo Html::activeHiddenInput($modeldetails, "[{$i}]id");
}
?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($modeldetails, "[{$i}]id_brg")->label('Nama')->widget(Select2::classname(), [
'data' => $brg,
'language' => 'en',
'options' => ['placeholder' => 'Select an item ...'],
'pluginOptions' => [
'allowClear' => true
],
]);?>
</div>
<div class="col-sm-6">
<?= $form->field($modeldetails, "[{$i}]qty")->textInput() ?>
</div>
</div><!-- .row -->
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div><!-- .panel -->
<?php DynamicFormWidget::end(); ?>
my models for details
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' => 'ID',
'kode' => 'Kode',
'id_brg' => 'ID Barang',
'qty' => 'Qty',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getLinkheader()
{
return $this->hasOne(Transaksi::className(), ['kode' => 'kode']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getItems()
{
return $this->hasOne(Barang::className(), ['id' => 'id_brg']);
}
}
Upvotes: 2
Views: 2052
Reputation: 410
Use custom validator. Simple example is inline validator:
public function rules()
{
return [
[['id_brg', 'qty'], 'required'],
[['kode', 'id_brg', 'qty'], 'integer'],
['qty', function ($attribute, $params) {
/* calculate min value */
$min = 123;
if ($this->$attribute > $min) {
$this->addError($attribute, "Qty must be less than {$min}.");
}
}],
];
}
Upvotes: 1