Maitray Suthar Maitray Suthar -3 years ago 140
PHP Question

'whenClient' not working with 'max' validation

i am working with Yii2 framework. I am making form to store promocodes. i have two fields named

discount_type
&
discount_value
.
discount_type
is a dropdown with options percentage & amount. I want to validate
discount_value
field depends on user selection of
discount_type
dropdown. If user selects percentage then discount_value field has validation for max value is 100.

_form.php
is:

<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>


Rules in Model is:

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";}'],
];
}


Getting this Error:

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}


I think problem is in
'max'=>'100'
when i use
'required'
instead it, it works fine. But i want to use 'max' validation. Please help to solve my issue.

Thanks in advance!

Answer Source

You didn't put the name of the validator in your rule. Instead of this:

[['promocode_value'], 'max' => '100','whenClient' => 'function (attribute, value) {return $("#discount_type").val() == "percentage";}']

Should be like this:

[['promocode_value'], 'number', 'max' => '100','whenClient' => 'function (attribute, value) {return $("#discount_type").val() == "percentage";}']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download