我正在使用Yii2框架。我正在制作存储promocodes的表格。我有两个名为discount_type
&的字段discount_value
。 discount_type
是带有期权百分比&的下拉列表量。我想验证discount_value
字段取决于用户对discount_type
下拉菜单的选择。如果用户选择百分比,则discount_value字段的最大值验证为100。
_form.php
是:
<div class="promocode-form">
<div class="row">
<div class="col-md-12">
<div class="col-lg-8">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'promo_name') ?>
<?php
$a= ['percentage' => 'Percentage', 'amount' => 'Amount'];
echo $form->field($model, 'discount_type')->dropDownList($a,['prompt'=>'Select Option']);
?>
<?= $form->field($model, 'promocode_value')->textInput(['type'=>'number']) ?>
<?= $form->field($model, 'promocode_limit')->textInput(['type'=>'number']) ?>
<?= $form->field($model, 'start_date')->textInput(['class' => 'form-control','placeholder'=>'Start Date','id'=>'dt1','readonly'=>"readonly",'value'=>$astart]) ?>
<?= $form->field($model, 'end_date')->textInput(['class' => 'form-control','placeholder'=>'End Date','id'=>'dt2','readonly'=>"readonly",'value'=>$aend]) ?>
<?= $form->field($model, 'status')->dropDownList(['1'=>'Active','0'=>'Deactive'],['prompt'=>'Select Option']) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
</div>
模型中的规则是:
public function rules()
{
return [
[['adminid', 'promo_name', 'discount_type', 'promocode_value', 'promocode_limit', 'start_date', 'end_date', 'status', 'created_at', 'updated_at'], 'safe'],
[['promo_name', 'discount_type', 'promocode_value', 'promocode_limit', 'start_date', 'end_date', 'status'], 'required'],
['promocode_value', 'number', 'min' => 0],
[['promocode_value'], 'max' => '100','whenClient' => 'function (attribute, value) {return $("#discount_type").val() == "percentage";}'],
];
}
出现此错误:
PHP User Error – yii\base\ErrorException
Exception (Invalid Configuration) 'yii\base\InvalidConfigException' with message 'Invalid validation rule: a rule must specify both attribute names and validator type.'
in /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/Model.php:450
Stack trace:
#0 /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/Model.php(411): yii\base\Model->createValidators()
#1 /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/Model.php(426): yii\base\Model->getValidators()
#2 /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/Model.php(472): yii\base\Model->getActiveValidators('promo_name')
#3 /var/www/html/AdventureYii/vendor/yiisoft/yii2/widgets/ActiveField.php(859): yii\base\Model->isAttributeRequired('promo_name')
#4 /var/www/html/AdventureYii/vendor/yiisoft/yii2/widgets/ActiveField.php(393): yii\widgets\ActiveField->addAriaAttributes(Array)
#5 /var/www/html/AdventureYii/vendor/yiisoft/yii2/widgets/ActiveField.php(204): yii\widgets\ActiveField->textInput()
#6 /var/www/html/AdventureYii/vendor/yiisoft/yii2/widgets/ActiveField.php(176): yii\widgets\ActiveField->render()
#7 /var/www/html/AdventureYii/backend/views/promocode/_form.php(28): yii\widgets\ActiveField->__toString()
#8 /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/View.php(328): require('/var/www/html/A...')
#9 /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/View.php(250): yii\base\View->renderPhpFile('/var/www/html/A...', Array)
#10 /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/View.php(152): yii\base\View->renderFile('/var/www/html/A...', Array, NULL)
#11 /var/www/html/AdventureYii/backend/views/promocode/create.php(17): yii\base\View->render('_form', Array)
#12 /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/View.php(328): require('/var/www/html/A...')
#13 /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/View.php(250): yii\base\View->renderPhpFile('/var/www/html/A...', Array)
#14 /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/View.php(152): yii\base\View->renderFile('/var/www/html/A...', Array, Object(backend\controllers\PromocodeController))
#15 /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/Controller.php(381): yii\base\View->render('create', Array, Object(backend\controllers\PromocodeController))
#16 /var/www/html/AdventureYii/backend/controllers/PromocodeController.php(74): yii\base\Controller->render('create', Array)
#17 [internal function]: backend\controllers\PromocodeController->actionCreate()
#18 /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#19 /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/Controller.php(156): yii\base\InlineAction->runWithParams(Array)
#20 /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/Module.php(523): yii\base\Controller->runAction('create', Array)
#21 /var/www/html/AdventureYii/vendor/yiisoft/yii2/web/Application.php(102): yii\base\Module->runAction('promocode/creat...', Array)
#22 /var/www/html/AdventureYii/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#23 /var/www/html/AdventureYii/backend/web/index.php(17): yii\base\Application->run()
#24 {main}
我认为问题出现在'max'=>'100'
当我使用'required'
代替它时,它运行正常。但我想使用&#39; max&#39;验证。请帮助解决我的问题。
提前致谢!
答案 0 :(得分:2)
您没有在您的规则中放置验证器的名称。而不是:
[['promocode_value'], 'max' => '100','whenClient' => 'function (attribute, value) {return $("#discount_type").val() == "percentage";}']
应该是这样的:
[['promocode_value'], 'number', 'max' => '100','whenClient' => 'function (attribute, value) {return $("#discount_type").val() == "percentage";}']