我在验证方面遇到了麻烦,
如何验证要插入的最小数量
$form->field($modeldetails, "[{$i}]qty")->textInput()
基于表数量字段的总和以及从$form->field($model, 'tgl')
过滤的参数,因为我不能简单地在模型规则中添加范围验证。
我需要一个函数来从两个模型($model,$modeldetails)
获取参数,并处理数量汇总,如果用户插入的金额超过最小金额,则在数量字段中显示错误。
查看标题的
<?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
],
]); ?>
查看有关详细信息的activeform字段
<?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(); ?>
我的模型了解详情
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']);
}
}
答案 0 :(得分:1)
使用自定义验证器。简单的例子是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}.");
}
}],
];
}
答案 1 :(得分:0)
核心验证器integer
[['qty'], 'integer', 'min' => 123],